Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am lost... Help me please

 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am quite new to java, below is my program, but i keep getting the error, please advise...
Error message :
java.lang.NullPointerException:
at tma02q3.AuctionItems.getAuctionItem(AuctionItems.java:35)
at tma02q3.AuctionTest.main(AuctionTest.java, Compiled Code)
Exception in thread "main"

Program :
public class AuctionItems{
Connection conn = null;
Statement stmt = null;
public AuctionItems(String protocol, String user, String driver, String dataBaseName) {
try{
Class.forName(driver);
Connection con = DriverManager.getConnection(protocol + dataBaseName);
Statement stmt = con.createStatement();
System.out.println("Database Connected");
}catch(ClassNotFoundException e){
System.out.println("Cannot load driver:"+e);
}catch(SQLException e){
System.out.println("Database connection failed:"+e);
}
}
public AuctionItem getAuctionItem(String itemID)throws SQLException{
String retrieveAllStr = "select * from AuctionData where ItemId ="+itemID;
ResultSet rs = stmt.executeQuery(retrieveAllStr);
String itemName = rs.getString("ItemName");
int reserve = rs.getInt("Reserve");
int currentBid = rs.getInt("CurrentBid");
AuctionItem getItem = new AuctionItem(itemName, reserve, currentBid, itemID);
rs.close();
return(getItem);
}
...........
...........
}
Thanks & Regards
Newbie
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you gave us the error:
java.lang.NullPointerException:
at tma02q3.AuctionItems.getAuctionItem(AuctionItems.java:35)

and some code.
We now know, there is a nullpointer-exception in line 35.
But the code copied to an editor, is only 31 lines.
It would be usefull to mark the line // here is line 35
Some users will not have an editor with Linenumbers by hand when reading the forum, that's why it's not sufficient to cut 'n' paste the whole code, including comments and trivial import-statements.
But it would be useful to surround codings by [ code] [ /code] - blocks (use the 'code'-Button.)
You can still do this now afterwards, by editing your post.
I see two outer variables, conn and stmt.
The conn isn't used, instead you create a 'con'.
The stmt is hidden by your newly introduced local 'Statement stmt'. This shall be the error, because later you refer to the outer stmt, which wasn't initialised.
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefan,
Thanks for your reply, i don't quite understand about your discussion on the stmt, sorry for the ignorance.. it would be nice if you could show example... thanks... i pointed out the line 35 below.
Error message :
java.lang.NullPointerException:
at tma02q3.AuctionItems.getAuctionItem(AuctionItems.java:35)
at tma02q3.AuctionTest.main(AuctionTest.java, Compiled Code)
Exception in thread "main"

Program :
public class AuctionItems{
Connection conn = null;
Statement stmt = null;
public AuctionItems(String protocol, String user, String driver, String dataBaseName) {
try{
Class.forName(driver);
Connection con = DriverManager.getConnection(protocol + dataBaseName);
Statement stmt = con.createStatement();
System.out.println("Database Connected");
}catch(ClassNotFoundException e){
System.out.println("Cannot load driver:"+e);
}catch(SQLException e){
System.out.println("Database connection failed:"+e);
}
}
public AuctionItem getAuctionItem(String itemID)throws SQLException{
String retrieveAllStr = "select * from AuctionData where ItemId ='"+itemID+"'";
ResultSet rs = stmt.executeQuery(retrieveAllStr); //Line 35
String itemName = rs.getString("ItemName");
int reserve = rs.getInt("Reserve");
int currentBid = rs.getInt("CurrentBid");
AuctionItem getItem = new AuctionItem(itemName, reserve, currentBid, itemID);
rs.close();
return(getItem);
}
...........
...........
}
Thanks & Regards
Newbie
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

try{
Class.forName(driver);
Connection con = DriverManager.getConnection(protocol + dataBaseName);
Statement stmt = con.createStatement();
System.out.println("Database Connected");

You're redefining stmt as a Statement object here. You've already declared it as a class attribute, so you do not need to redefine it in the constructor. Just get rid of Statement in the above code and it should work.
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeffrey,
I got it.. but now there's even more errors when i run the program.. any suggestions.... thanks
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:4089)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:4246)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:1902)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:1774)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:185)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:241)
at tma02q3.AuctionItems.getAuctionItem(AuctionItems.java:37)
at tma02q3.AuctionTest.main(AuctionTest.java, Compiled Code)
Exception in thread "main"
Thank you & Regards
Newbie
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet rs = stmt.executeQuery(retrieveAllStr);
String itemName = rs.getString("ItemName");

When the resultset object is created it is by default on Before First position. So you will have to move the cursor one state next. Use Following Code instead
ResultSet rs = stmt.executeQuery(retrieveAllStr);
rs.next(); //move to first record
String itemName = rs.getString("ItemName");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic