views:

136

answers:

4

Hi,

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

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

A: 

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

Codemwnci
A: 
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();
Cold-Blooded
java does have System.console() since 1.6 - check current documentation of java.lang.System.
foo
A: 

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).

foo
true, but if readLine returns null, it will simply add null to the arraylist, and not throw a null pointer exception.
Codemwnci
You are right - ArrayList is one of those collections that just stores the null value. Others will throw an exception. You should check it anyways, because if you get a "null" result, that's not something you can use for string operations later on, so even if you don't get the Exception when storing the null, you'll get it when trying to use the data. And the farther away the Exception is from its cause, the harder to understand what happend. Better check it right there.
foo
A: 

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!

Topera