A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Databases
»
JDBC
Author
Nullpointer exception?
Jyoti Vaskar
Ranch Hand
Joined: Jun 30, 2009
Posts: 142
posted
Sep 28, 2009 05:40:47
0
Can anybody solve this problem???
I am trying to store the data from database(table jyoti) in a bean
& then adding the bean into an arraylist.
here is my code:
package web; import java.sql.*; import java.util.*; public class DAO { ArrayList al; Connection con; ResultSet rs; Statement st; public Connection getConnection() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:MyData"); } catch(ClassNotFoundException cnfe) {} catch(SQLException se) {} return con; } public ArrayList getData() { try { con=getConnection(); st=con.createStatement(); rs=st.executeQuery("select * from jyoti"); while (rs.next()) { EntityBean eb=new EntityBean(); String name=rs.getString(1); eb.setName(name); int age=rs.getInt(2); eb.setAge(age); String sex=rs.getString(3); eb.setSex(sex); System.out.println(eb); System.out.println(al); al.add(eb); System.out.println(al); } rs.close(); st.close(); con.close(); } catch (SQLException sql) {} return al; } }
what's wrong with the above code?
I am getting Nullpointer exception?
it's stucking up after
System.out.println(al);
(above line prints null) for al.add(eb) it stucks?
?
thanks
Jyo
Sean Clark
Rancher
Joined: Jul 15, 2009
Posts: 377
I like...
posted
Sep 28, 2009 05:47:20
0
Hey,
You haven't initialised the
ArrayList
anywhere.
Sean
I love this place!
Jyoti Vaskar
Ranch Hand
Joined: Jun 30, 2009
Posts: 142
posted
Sep 28, 2009 05:49:28
0
where it matters ?
doesn't it take the default value null?? As its instance variable??
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
I like...
posted
Sep 28, 2009 07:28:51
0
Yes, but you can't call methods on a variable that is null.
JavaRanch FAQ
HowToAskQuestionsOnJavaRanch
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
I like...
posted
Sep 29, 2009 04:38:47
0
Objects start as null until they are initialized. Since an
ArrayList
is an object, you must initialize it else its just a null pointer.
My Blog:
Down Home Country Coding with Scott Selikoff
Jyoti Vaskar
Ranch Hand
Joined: Jun 30, 2009
Posts: 142
posted
Sep 30, 2009 11:38:14
0
thank you guys
.
I got my problem & I'm sort with it.
thank you!
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Nullpointer exception?
Similar Threads
Access database from a class file
Unable to find specific product
File Operations Program Problem
insert statement is not working
Problems when viewing specific description when search
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter