aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes NPE in Eclipse, but not in commandline? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "NPE in Eclipse, but not in commandline?" Watch "NPE in Eclipse, but not in commandline?" New topic
Author

NPE in Eclipse, but not in commandline?

Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Hi guys.

I'm new to Eclipse and currently trying to get comfortable using it since it will be my main development tool when I start my next job in a couple weeks.
Have a look at this code please, it's very simple.



I've compiled and run it using a dos-prompt without problems, but for some reason eclipse gives me a NPE when i try to execute the same thing in the IDE:


Exception in thread "main" java.lang.NullPointerException
at consoletest.Con1.main(Con1.java:9)


Has anyone got a clue why?

Cheers,
Andreas.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

My guess: did Eclipse stop on line 8 and ask you for the input? I suspect it did not. Thus s1 would be null.


JBoss In Action
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Peter Johnson wrote:My guess: did Eclipse stop on line 8 and ask you for the input? I suspect it did not. Thus s1 would be null.


Indeed it did not, but how come? Is there something "wrong" with the Console from System.console() when we are executing from inside Eclipse? Do we not have a Console at our disposal in Eclipse?

// Andreas
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
I tried executing the same program in NetBeans, appearently it causes the same NPE so I take it it's a "problem" for all IDEs. I can only assume it's because of what I stated above, we don't have a Console at our disposal in an IDE, although I'm unsure why that is.

// Andreas
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

Usually tools provide some mechanism to pass a text file as stdin to an app, but I don't see where that is done in Eclipse. I'm looking under Run Configurations for a Java Application, but nothing is there. Therefore, stdin would map to null, and you would not get any input.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

I think I might have found it in Eclipse. Right-click the project and select Properties > Run/Debug Settings > New > Java Application > OK then on the Common tab you see what I attached below. Thus create a run configuration for your app and try it again.


[Thumbnail for jr.png]

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

OK, that suggestion works only if you are reading from System.in, not if you create a read from a Console. And actually, I am getting the error one line earlier - the Console object is null!
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

Looking at the documentation for Console I see this statement:

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.


I think that the second sentence is the key. When running within Eclipse, stdin and stdout will have been redirected so the console will not be available.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Peter Johnson wrote:Looking at the documentation for Console I see this statement:

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.


I think that the second sentence is the key. When running within Eclipse, stdin and stdout will have been redirected so the console will not be available.


Hi again.

Well, first off I'll admit I haven't used the Console class much so not really familiar its particulars - however a couple of things strike me as a little weird here.

First off, like you said, if I go into Properties > Run/Debug Settings and edit the one currently there, I can confirm that "Allocate console" is already ticked in - so why do we still not have access to a Console object?

Also, I find it a bit weird that we are able to read input from the user/keyboard using a Scanner and System.in, if we so please, but not using a Console object. As I said this probably has to do with the particulars of Console which I am not familiar with, it just seems a bit unlogical to me.

EDIT: A last question btw, you mention stdin and stdout; are you referring to System.in and System.out or are these two another pair of streams ?

// Andreas

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

Console appears to have been added to the API in Java SE 6, so it could have some peculiarities. For example, I suspect that if you ran your app with javaw that there would be no Console object, but with java there would be. Before Java SE 6, System.in was the only way to get access to stdin. And whether the app was run with javaw or java, both System.in and System.out are set (I think..)

stdin, stdout and stderr are the common names for the files referenced by System.in, System.out and System.err, respectively.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NPE in Eclipse, but not in commandline?