开发者

Why am I getting a null pointer exception

开发者 https://www.devze.com 2023-01-16 21:42 出处:网络
Today is my first day learning java :) I\'m having problems running a very simple example (not a great start).

Today is my first day learning java :)

I'm having problems running a very simple example (not a great start).

It's just a simple example that asks a user for input and prints it back out but I'm getting a null pointer exception when I try to read a line from the console.

I don't understand because everything seems to be instantiated.

 public static void main(String[] args) {
        // TODO code application logic here

        Console console = System.console();
        String userinput;
         userinput= console.readLine("Enter input: ");
        /* Creates list for planets */
        ArrayList outputlist= new ArrayList();
        outputlist.add(userinput); // Adds users input to the list
        outputlist.add("an entry"); // Adds a string to the list
        System.out.println("\nTwo items: " + outputlist);
    } 

EDIT 1

As a number of people have pointed out the error is thrown when I try to read a line from the console because console is null (even though I'm instantiating it ?).

I feel a bit silly asking this but how can I make the console "not null". Which I thought I was doing by using Console console = System.console().

My expectation of workflow was to write a simple user input using netbeans. Hit the debug button. See a screen pop up. Input some text. See the output.

EDIT 2

开发者_JAVA技巧

O.K

After a little digging around it turns out that you cannot use system.console within netbeans. I don't understand why. I just user scanner instead.

Now I'm not sure what answer to accept o-0


It has to be System.console(). You are not doing any other operation on an object that would cause a null pointer exception. And as @McDowell has pointed out, System.console() can return a null value


Console console = System.console();
String userinput;
userinput= console.readLine("Enter input: ");

instead of this use the following code; i think System.console is used in .Net platform

String userinput;
InputStreamReader sr =new InputStreamReader(System.in);
BufferReader br=new BufferReader(sr);
userinput=br.readLine();


Run this in a terminal (shell in linux or cmd in windows).

In my case, I put your code in a file (Test.java). After build, IDE (eclipse, in my case) creates a bin file (Test.class)

So, just go to this folder and call:

$ java Test
Enter input: asdasd

Two items: [asdasd, an entry]

And works!


You are not checking any of the result values. So, if one of those calls returns "null" and you try to subsequently use it as if it were a reference/pointer to an object, that's where your NullPointerException comes from.

The likely culprits are System.console() and console.readLine().

http://download.oracle.com/javase/6/docs/api/java/io/Console.html#readLine%28%29 says readLine may return null. http://download.oracle.com/javase/6/docs/api/java/lang/System.html#console%28%29 too says this call may return null. So better check the values returned in both cases before using them.

(As you should do in general, if the function specifies such possible results).

0

精彩评论

暂无评论...
验证码 换一张
取 消