尝试将数据添加到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;
}
}
}
记录Cat错误
在resources.CoursesSupportBean.getDb_Data(CoursesSupportBean.java:109)的java.lang.NullPointerException
答案 0 :(得分:3)
您正在构造函数中将allData
ArrayList
设置为null
。删除这一行:
allData = null;
从那里开始。通常,如果要在构造函数中初始化变量,只需在构造函数外部的类中声明它们,并仅在构造函数中初始化它们。换句话说,将以上allData = null;
行替换为:
allData = new ArrayList<Object>();
在构造函数之外替换此行:
private List<Object> allData = new ArrayList<Object>();
用这个:
private List<Object> allData;
答案 1 :(得分:2)
ArrayList由定义初始化为字段,此处为:
private List<Object> allData = new ArrayList<Object>();
但是你似乎在构造函数中将allData设置为null:
public CoursesSupportBean() {
url="";
DataSourceName="";
error="";
allData = null;
course_name="";
courseId=0;
obj=null;
}
所以你得到一个空指针异常,因为你正在设置已经初始化为null的ArrayList引用allData(不再指向创建的ArrayList)
只需删除此行:
allData = null;
通常,您初始化构造函数中的所有成员,但集合的初始化通常保留在字段定义中,除非它是一个昂贵的操作,因此尽可能地延迟。