aspose file tools*
The moose likes Beginning Java and the fly likes Null pointer Exception error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null pointer Exception error" Watch "Null pointer Exception error" New topic
Author

Null pointer Exception error

ed spithaler
Greenhorn

Joined: Feb 29, 2012
Posts: 9
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
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?


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
ed spithaler
Greenhorn

Joined: Feb 29, 2012
Posts: 9
I changed the code and I know longer have the error. Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null pointer Exception error