import java.io.*;



public class Test {

public static void main(String[] arg){
	Console console=System.console();	   	     
	String chessPiece="";
	int rowEnd=0;	
	char columnEnd = ' ';   

       System.out.println("what piece do you want to move?");  
       chessPiece=console.readLine();
		
	   System.out.println("where do you want to move it to?(type column letter)");
	   String input=console.readLine();
	   columnEnd=input.charAt(0);
	   System.out.println("where do you want to move it to?(type row number)");
	   rowEnd=Integer.parseInt(console.readLine());
   } 
}

if i put this into a notepad and launch it, it works, but with eclipse it tells me:

what piece do you want to move?
Exception in thread "main" java.lang.NullPointerException
	at Test.main(Test.java:14)

why??

Recommended Answers

All 7 Replies

Is the console variable null?
Read the API doc for the System console() method. Can that method return null?

I don't know why your IDE doesn't support the console method.

it is hard to say norm1. why wouldnt eclipse support Console console=System.console();?

try this code to check if your eclipse supports it... my eclipse doesn't support console also.

package console;

//import java.io.*;

public class Console {

	public static void main(String[] args) {
		
		java.io.Console console = System.console();
		if (console == null) { System.out.println("unable to obtain console:(");
		return;
		}
			String name = console.readLine();
			System.out.println(name);
		
		
	}

}

otherwise use bufferedreader and inputstreamreader

BufferedReader keyboard;
keyboard = new BufferedReader(new InputStreamReader(System.in));
String input = keyboard.readLine();

If it doesn't support something and you *must* do it that way, get another IDE temporarily or for good

Read the Javadocs for the Console class; it has the answer for your "why".

[...] 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 transfered everything on a notepad and i test my code from there..

it is two weird that eclipse, didnt put that function

I know this is marked "solved", but here's some more info that may be helpful.
1. The console problem is generic to IDE's. NetBeans has it as well. It's a Java thing, as ~s.o.s~ noted.
2. You can achieve the same result easily by using a BufferedReader on System.in as follows:

try {
         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Type name: ");
         String name = reader.readLine();
         System.out.println("Hello " + name);
      } catch (IOException e) {
         e.printStackTrace();
      }

This works perfectly under the current Eclipse/Java versions on Win 7.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.