當我嘗試將數據添加到ArrayList時,我總是收到一個空點異常。我在網上看過,所有的答案都說確保你初始化ArrayList,但是我仍然得到一個空指針異常。有任何想法嗎?ArrayList拋出空指針期望
package resources;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.naming.*;
import javax.servlet.http.*;
public class CoursesSupportBean
{
private String url;
private String DataSourceName;
String error;
private List<Object> allData = new ArrayList<Object>();
public String course_name;
public int courseId;
Object obj=new Object();
public CoursesSupportBean() {
url="";
DataSourceName="";
error="";
allData = null;
course_name="";
courseId=0;
obj=null;
}
public void setObj(Object obj) {
this.obj = obj;
}
public String getError() {
return error;
}
public void setUrl(String url){
this.url = url;
}
public void setDataSourceName(String DataSourceName){
this.DataSourceName=DataSourceName;
}
public List getDb_Data()
{
String framework = "embedded";
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String protocol = "jdbc:derby:";
//HttpSession session = req.getSession();
//String url = (String)session.getAttribute("url");
//String DataSourceName = (String)session.getAttribute("DataSourceName");
//res.setContentType("text/html");
//PrintWriter out = res.getWriter();
Context ctx = null;
java.sql.Connection conn = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,url);
ArrayList statements = new ArrayList(); // list of Statements, PreparedStatements
PreparedStatement psInsert = null;
PreparedStatement psUpdate = null;
Statement s = null;
ResultSet rs = null;
int i=0;
try
{
ctx = new InitialContext(ht);
try {
ctx.createSubcontext(DataSourceName);
}
catch (NameAlreadyBoundException e) {
// Subcontext already exists.
// Note that WebLogic's Context implementation does not throw this
// exception if the name is already bound to an identical object.
}
String dbName = "JHU";
try {
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup(DataSourceName);
conn = ds.getConnection();
conn.setAutoCommit(false);
s = conn.createStatement();
statements.add(s);
}
catch (SQLException e) {
System.out.println("----- SQLException -----");
System.out.println(" SQL State: " + e.getSQLState());
System.out.println(" Error Code: " + e.getErrorCode());
System.out.println(" Message: " + e.getMessage());
}
}
catch (NamingException e) {
System.out.println("Naming Exception");
}
try
{
rs = s.executeQuery("SELECT * FROM COURSES");
if (rs == null)
{
System.out.println("No rows in ResultSet");
}
while (rs.next())
{
DataFields d=new DataFields(rs.getInt(1), rs.getString(2));
System.out.println("Course data: "+ rs.getInt(1) + " " + rs.getString(2));
this.allData.add(i,d);
i++;
}
}
catch (SQLException e) {
System.out.println("----- SQLException -----");
System.out.println(" SQL State: " + e.getSQLState());
System.out.println(" Error Code: " + e.getErrorCode());
System.out.println(" Message: " + e.getMessage());
}
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
System.out.println("----- SQLException -----");
System.out.println(" SQL State: " + e.getSQLState());
System.out.println(" Error Code: " + e.getErrorCode());
System.out.println(" Message: " + e.getMessage());
}
// Statements and PreparedStatements
int c=0;
while (!statements.isEmpty()) {
// PreparedStatement extend Statement
Statement st = (Statement)statements.remove(c);
try {
if (st != null) {
st.close();
st = null;
}
} catch (SQLException e) {
System.out.println("----- SQLException -----");
System.out.println(" SQL State: " + e.getSQLState());
System.out.println(" Error Code: " + e.getErrorCode());
System.out.println(" Message: " + e.getMessage());
}
}
//Connection
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
System.out.println("----- SQLException -----");
System.out.println(" SQL State: " + e.getSQLState());
System.out.println(" Error Code: " + e.getErrorCode());
System.out.println(" Message: " + e.getMessage());
}
return this.allData;
}
public int getCourseId()
{
this.courseId=((DataFields)obj).courseId;
return this.courseId;
}
public String getCourse_Name() {
this.course_name=((DataFields)obj).course_name;
return this.course_name;
}
public class DataFields
{
public String course_name;
public int courseId;
public DataFields(int courseId, String course_name)
{
this.courseId=courseId;
this.course_name=course_name;
}
}
}
登錄貓錯誤
在resources.CoursesSupportBean.getDb_Data(CoursesSupportBean.java:109)顯示java.lang.NullPointerException
放入包含堆棧跟蹤的完整異常,並說明代碼中的哪一行對應於堆棧跟蹤中的哪一行。 –
應該是更具體一點。 this.alldata.add(i,d);是拋出異常的線。這是109線。我已經嘗試過,並與「這個」。描述符。 – user3344646
請*編輯*的問題,而不是發佈5 stacktrace評論! – andyb