run time error... thread

hi, all, i got a strange problem on my program..i could compiler my program..but i can`t get it run properly...pls help

import java.util.InputMismatchException;

import java.util.NoSuchElementException;

import java.util.Scanner;

publicclass Test{

private Scanner theKbd;

Test()

{

}

private String readString(String userInstruction)

{

String aString =null;

try

{

System.out.print(userInstruction);

aString = theKbd.nextLine();

}

catch (NoSuchElementException e)

{

//if no line was found

System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);

}

catch (IllegalStateException e)

{

// if this scanner is closed

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return aString;

}

publicstaticvoid main(String[] args)

{

Scanner scan =new Scanner(System.in);

String userInput;

Test example1 =new Test();

System.out.println("Testing readString function Demo");

userInput = example1.readString("Please your String");

System.out.println("Your input is " + userInput);

}

//the result come up as following

// Testing readString function Demo

//

// Please your StringException in thread "main" java.lang.NullPointerException

//at Test.readString(Test.java:22)

//at Test.main(Test.java:46)

}

[2845 byte] By [Ivan1238a] at [2007-11-27 5:33:12]
# 1

I think you are confused between variables theKbd and scan. I editted your code so it works:

import java.util.InputMismatchException;

import java.util.NoSuchElementException;

import java.util.Scanner;

public class Test{

private Scanner theKbd = new Scanner(System.in);

private String readString(String userInstruction) {

System.out.print(userInstruction);

try {

return theKbd.nextLine();

} catch (NoSuchElementException e) {

//if no line was found

System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);

} catch (IllegalStateException e) {

// if this scanner is closed

System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);

}

return null;

}

public static void main(String[] args) {

Test example1 = new Test();

System.out.println("Testing readString function Demo");

String userInput = example1.readString("Please your String");

System.out.println("Your input is " + userInput);

}

}

Hippolytea at 2007-7-12 14:59:56 > top of Java-index,Java Essentials,New To Java...