2 Replies - 7766 Views - Last Post: 24 December 2008 - 10:55 AM Rate Topic: -----

#1 amit sandhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-December 08

Exception in thread "main" java.lang.NullPointerException

Posted 24 December 2008 - 10:26 AM

import java.util.Scanner;
public class test{
	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		int age;
		double price = 0.00;
		char reply;
		System.out.print("How old are you? ");
		age = scan.nextInt();
		System.out.print("Have a coupon? (Y/N) ");
		reply = scan.findInLine(".").charAt(0);
		 if (age >= 12 && age < 65){
			price = 9.25;
		}
		if (age < 12 || age >= 65){
			price = 5.25;
		}
		if (reply == 'y' || reply == 'Y'){
			price -= 2.00;
		}
		if (reply != 'Y' && reply != 'y' && reply
			 != 'N' && reply != 'n'){
			System.out.println("Huh?");
		}
		System.out.print("Please pay ");
		System.out.print(price);
		System.out.print(".");
		System.out.println("Enjoy the show!");
	}
}


*edit: Please use code tags in the future, thanks! :)


*/
its very very simple code but i keep geting this error;
Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:11)
/*

This post has been edited by Martyr2: 24 December 2008 - 10:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Exception in thread "main" java.lang.NullPointerException

Posted 24 December 2008 - 10:35 AM

I just got done telling you in MSN messenger to READ before you do things. You didn't read the instructions... and therefor you probably won't get much help. When you post your code, you need to use :code: tags so we can read your code.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Exception in thread "main" java.lang.NullPointerException

Posted 24 December 2008 - 10:55 AM

As for your problem, you are using nextInt to collect the age so that is going to read in the number and leave in a carriage return. This carriage return is going to mess with your scan.findInLine function because it is ONLY going to read in the remaining carriage return instead of your next input parameter.

Just to show you that this is the case try the following lines (this isn't a solid solution, only an example to show you that the carriage return is indeed the problem)

age = scan.nextInt();
System.out.print("Have a coupon? (Y/N) ");

// This line is going to eat up that carriage return and throw it away
scan.nextLine();

// Now it will go ahead and evaluate your next input and grab the first character
reply = scan.findInLine(".").charAt(0);



I am not quite sure you use findInLine in this way, you should just read in each line (using scanner nextLine) for both the age and have coupon and parse it to an integer (using Integer.parseInt()) in the first case and then use charAt(0) on the second case.

System.out.print("How old are you? ");

// Parse the line read into an integer
age = Integer.parseInt(scan.nextLine());

System.out.print("Have a coupon? (Y/N) ");

// Get nextLine and then pull off the first character to test for reply
reply = scan.nextLine().charAt(0);




Hope this makes sense to you. Enjoy!

"At DIC we be scanner reading code ninjas...we even have one of those eye scanners that grants us access to the DIC site. Retinal scans are da bomb and we coded it in C++ because C++ can code anything!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1