0

The Problem:

java.lang.NullPointerException at mainPackagePaketverwaltung.Datenbank.savePaket(Datenbank.java:58) at mainPackagePaketverwaltung.MainPV$12.actionPerformed(MainPV.java:507)

The Cause:

This is the Line (58):

myStatement.executeUpdate("INSERT INTO Relation VALUES(NULL, '"+ PaketID +"', '"+ atodb.getArtikelID() + "', '"+ atodb.getMenge() +"')");

From what i understand this should create a new line in the Database because the NULL/Primary_Key is set to Auto_Increment, but after one runt it errors out.

That should not happen bc with each run a new line would be made by the Auto_Increment.

More Info:

PaketID is a INT

atodb.getArtikelID is a INT

atodb.getMenge is a INT

All DB Fields are INT,s

LifeLifeLP
  • 23
  • 4
  • 2
    are you sure that `atodb` is not null? – Youcef LAIDANI May 08 '18 at 09:42
  • and also `myStatement` should not be null – guleryuz May 08 '18 at 09:46
  • @YCF_L Yes, i can confirm (just checked with a System.out.print) they are not null, the first set of data makes it to the DB but the seconds errors out even with data in it – LifeLifeLP May 08 '18 at 09:47
  • @guleryuz i set it to null after the .executeUpdate is that wrong? – LifeLifeLP May 08 '18 at 09:48
  • 3
    Yes...that seems wrong – gargkshitiz May 08 '18 at 09:49
  • can you post `savePaket` method body – guleryuz May 08 '18 at 09:52
  • @gargkshitiz yep, that fixed it, i did never even think about it because in my mind that should not have be a problem if its after the execute bet there we are. Thx for the Quick help! :) – LifeLifeLP May 08 '18 at 09:52
  • can you show us the full code please, @gargkshitiz is correct, what if your transaction is not yet committed and you already set the object to null? and please read about ¨Prepared Statement your code is a victim of SQL Infection and error syntax – Youcef LAIDANI May 08 '18 at 09:53
  • @gargkshitiz `public static boolean savePaket(int PaketID, Artikel atodb) { try { myCon=DriverManager.getConnection(url, user, pwd); System.out.println(atodb.getArtikelID()); System.out.println(atodb.getMenge()); myStatement.executeUpdate("INSERT INTO Relation VALUES(NULL, '"+ PaketID +"', '"+ atodb.getArtikelID() + "', '"+ atodb.getMenge() +"')"); //myStatement=null; <= Dont do that anymore! myRS=null; return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }` – LifeLifeLP May 08 '18 at 09:56
  • @LifeLifeLP Please put the code as an edit into your question and not in the comments. without proper indention it's a nightmare to read. – L.Spillner May 08 '18 at 09:59
  • @L.Spillner Sorry, i was to late before this was closed, i will do it however next time i asksometing here :S – LifeLifeLP May 08 '18 at 10:00
  • myCon , myStatement seem to be class level variables, hence assigning null to them means the 2nd method call will throw NPEs – gargkshitiz May 08 '18 at 10:01

1 Answers1

0

Please ensure following

  • myStatement is not null

  • atodb is not null

Gagan Chouhan
  • 312
  • 1
  • 6