Java NullPointerException

S

Sasku

Gast
Hallo zusammen,

ich möchte mein Programm von der Console aus starten.
und möchte dazu ein passwort eingeben lassen. Allerdings bringt er mir, nachdem ich mir der readPassword(); methode einlesen will eine exception ( nullpointer ). Jetzt weiß ich nicht warum. Hier mein Code;:

Code:
  public static void main(String[] args) {
     s = new Scanner(System.in);
     dbc = new DBConnection();
     Console console = System.console();

     System.out.print("Auf welche Datenbank wollen Sie sich verbinden? ");
     dbname = s.nextLine();
     System.out.print("Welcher User verbindet sich? ");
     user = s.nextLine();
     System.out.print("Haben Sie ein Passwort? ");
     passString = console.readPassword();
     pw = new String(passString);
//nachfolgender Code

In der zeile
Code:
 passString = console.readPassword();
bringt er mir die Exception ... ich weiß aber nicht warum. HILFE!
 
Code:
 import java.io.Console;
import java.io.ObjectInputStream.GetField;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import DBConnection.DBConnection;

public class mainos {

	static DBConnection dbc;

	static Scanner s;

	static String dbname;
	static String pw;
	static String user;
	
	static char[] passString;
//	static Console cs;

	static ResultSet rs;

	public static void main(String[] args) {
		s = new Scanner(System.in);
		dbc = new DBConnection();
		Console console = System.console();

		System.out.print("Auf welche Datenbank wollen Sie sich verbinden? ");
		dbname = s.nextLine();
		System.out.print("Welcher User verbindet sich? ");
		user = s.nextLine();
		System.out.print("Haben Sie ein Passwort? ");
		passString = console.readPassword();
		pw = new String(passString);

// nachfolgender Code, welcher nicht zum Problem beiträgt


Fehlermeldung: "Exception in thread "main" java.lang.NullPointerException
at mainos.main(mainos.java:36)"
 
immer ^^ im debugger hab ich dann auch gesehen dass er die console nicht initialisiert ( also bei System.console(); sollte er ja die Console initialisieren .. aber irgendwie will das nich )
 
Kann er aber nicht, wenn du das in Eclipse ausführst. Da wirst du immer null zurückbekommen bei system.console(), weil der Eclipse-Debugger keine Console zurückliefern kann.

Wenn du die Datei direkt mit java ausführst, wirds klappen
 
passString ist doch ein array, wieso kommt dann kein Zugriff über den Index?

passString[0] = console.readPassword();

Ich benutze char in Java so gut wie nie. Wieso benutzt du nicht einfach einen String?
 
das macht er, weil die readPassword ein char array zurückliefert. Siehe Java Specs. Das ist auch nicht das Problem, das Problem hab ich schon erwähnt
 
Weil Console.readPassword() ein char Array zurückgibt... Kann man übrigens einfach nachschauen.

und auch
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.

http://docs.oracle.com/javase/7/docs/api/java/io/Console.html

/e zu spät :/

und ein Forum reicht btw... http://www.java-forum.org/thema/nullpointerexception.169652/#post-1067320
 
Zurück
Oben