• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer Exception error

 
ed spithaler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what this error means. Can anybody shed some light on it? Thanks

import java.util.Scanner;
/**

*/

public class Viewer
{
String customer;
String movieCode;
private double cost;

public Viewer (String customer, String movieCode)
{
customer = " ";
computeCost ();
cost = 0.0;
}
public String getReq()
{
return customer + " " + movieCode + " " + cost;
}

public void computeCost ()
{
double movieCost = 3.95;

char c = movieCode.charAt(0); //Exception in thread "main" java.lang.NullPointerException

switch (c)


{
case 'A': cost = movieCost* 1.50;
break;
case 'B': cost = movieCost*1.36;
break;
case 'C': cost = movieCost*1.26;
break;
case 'D': cost = movieCost*1.06;
break;
case 'F': cost = movieCost*1.00;
break;

default:
System.out.println("Error");


}

if (c =='N')
{
cost = movieCost + 2.50;
}
else
{
cost = 9999.99;
}

if (c == 'R')
{
cost = movieCost + 1.00;
}
else
{
cost = 9999.99;
}
}


}

import javax.swing.JOptionPane; // Needed for JOptionPane

/**
*/

public class ViewerTest
{
public static void main(String[] args)
{
// Get the user's first name.

String fLName = JOptionPane.showInputDialog("Enter your first and last name " );

String MC = JOptionPane.showInputDialog("Enter movie code:" );

MC = MC.toUpperCase();

Viewer nViewerTest = new Viewer(fLName, MC);

System.out.println(nViewerTest.getReq());

System.exit(0);
}
}

 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ed spithaler wrote:I don't understand what this error means. Can anybody shed some light on it? Thanks
...
char c = movieCode.charAt(0); //Exception in thread "main" java.lang.NullPointerException

Yes. 'movieCode' is null. Probably because you haven't initialized it to anything.

Winston
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that nViewerTest.movieCode has not been initialized, it's null. It seems like you've initialized it because you obtained an input from the user called MC, and you passed that String to the Viewer constructor that has a String argument called movieCode, but that argument wasn't used in the constructor. There are 2 variables with the same name. One movieCode is local to the constructor, and the other movieCode is a class Viewer instance variable.

A statement commonly seen in the constructor that would solve this problem is:

this.movieCode = movieCode;

This means that the Viewer instance variable movieCode is assigned the value passed to the constructor in the argument (local) variable movieCode.

Make sense?
 
ed spithaler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code and I know longer have the error. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic