NPE - Doch warum?

Java-Problems

Bekanntes Mitglied
Java:
public class Build {

	JFrame frame;
	Popup popup;
	JPanel panel = new JPanel();
	JTextField tf_input = new JTextField();
	JLabel lb_input = new JLabel();
	JButton bt_input = new JButton();
	String pfad;

	// Anzeige-Fenster für DB-Pfad
	public Build() {
		frame = new JFrame("Datumskonvertierung");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(350, 130);
		frame.setLocation(130, 75);

		panel.setLayout(null);
		lb_input.setBounds(10, 10, 250, 20);
		lb_input.setVisible(true);
		lb_input.setText("Bitte hier den Pfad der Datenbank eintragen:");
		tf_input.setBounds(10, 35, 250, 20);
		tf_input.setVisible(true);
		bt_input.setBounds(10, 60 , 100, 20);
		bt_input.setText("OK");
		bt_input.setVisible(true);
		bt_input.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pfad = tf_input.getText();
				System.out.println(pfad);
				
			}
		});
		
		panel.add(lb_input);
		panel.add(tf_input);
		panel.add(bt_input);
		
		frame.add(panel);
		frame.setVisible(true);
	}

	// AB hier: DB-Verbindung!

	public Connection open() {
		Connection con = null;
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// set this to a MS Access DB you have on your machine
			//String filename = "C:/Users/p.nebel/Desktop/Kundenstamm.mdb";
			String filename = pfad;
			String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
			//String url = "jdbc:odbc:C:/Users/p.nebel/Desktop/Kundenstamm.mdb";
			String url = "jdbc:odbc:"+filename;
			database += filename.trim() + ";DriverID=22;READONLY=false}";
			// database+= filename.trim() + ";DriverID=22}";

			con = DriverManager.getConnection(database, "", "");
		} catch (Exception e) {
			System.out.println("Error: " + e);
		}

		return con;
	}

	public void close(Connection con) {
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
[/Java]
[code=Java]
public class Connection2 {

	public static void main(String[] args) {

		Connection connect = null;
		String id = "";
		String datum_old = "";
		String datum_neu = "";
		String jahr = "";
		String monat = "";
		String tag = " ";
		int count = 0;
		int updateCount = 0;

		

		try {
			Build build1 = new Build();
			connect = build1.open();
			Statement s = connect.createStatement();
			Statement stmt = connect.createStatement();
			s.execute("select * from Kundenbezüge12_07_10");
			ResultSet rs = s.getResultSet();

			String[] reihe = new String[2];

			if (rs != null) // if rs == null, then there is no ResultSet to view
				while (rs.next()) // this will step through our data row-by-row
				{
					reihe[0] = rs.getString(1);
					reihe[1] = rs.getString(8);

					id = reihe[0];
					datum_old = reihe[1];

					if (datum_old.startsWith("6")) {
						jahr = "2006";
						monat = datum_old.substring(1, 3);
						tag = datum_old.substring(3, 5);

						datum_neu = tag + "-" + monat + "-" + jahr;

						 System.out.println(id + " : " + datum_old + ":" +
						 jahr + " " + monat + " " + tag);}
					

					String sql = "UPDATE Kundenbezüge12_07_10 SET Datum2 = '"
							+ datum_neu + "' WHERE ID = " + id;
					System.out.println(sql);
					updateCount = stmt.executeUpdate(sql);
					build1.close(connect);

				}

		    System.out.println(updateCount);
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}


	}

}

NPE wird in der Zeile vom Statement s = connect.createStatement(); angezeigt
 
Zuletzt bearbeitet:
überprüfe ob nochmal ob connect an dieser stelle null ist

und ob die Exception in open wirklich nicht fliegt...
wird System.out.println("Error: " + e); nicht aufgerufen?
 
du stellst wahrscheinlihc die verbinung nicht richtig her ;-)

Java:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // set this to a MS Access DB you have on your machine
            //String filename = "C:/Users/p.nebel/Desktop/Kundenstamm.mdb";
            String filename = pfad;
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
            //String url = "jdbc:odbc:C:/Users/p.nebel/Desktop/Kundenstamm.mdb";
            String url = "jdbc:odbc:"+filename;
            database += filename.trim() + ";DriverID=22;READONLY=false}";
            // database+= filename.trim() + ";DriverID=22}";
 
            con = DriverManager.getConnection(database, "", "");
bereinig das mal, da benutzt ja die hälfte variablen gar nicht!
 
Mach ein sinnvolles Exceptionhandling und Logging, dann erübrigt sich die Frage. Auch ein Debugger leistet unglaublich gute Dienste.

Nach einem potentiell schwerwiegendem Fehler (beim Holen der Connection) nur eine Meldung auszugeben und null zurückzugeben ist Käse, besonders wenn nach dem Aufruf der Methode nicht explizit gegen null getestet wird.
 

Zurück
Oben