2014-02-23 46 views
0

當我嘗試將數據添加到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

+1

放入包含堆棧跟蹤的完整異常,並說明代碼中的哪一行對應於堆棧跟蹤中的哪一行。 –

+0

應該是更具體一點。 this.alldata.add(i,d);是拋出異常的線。這是109線。我已經嘗試過,並與「這個」。描述符。 – user3344646

+2

請*編輯*的問題,而不是發佈5 stacktrace評論! – andyb

回答

2

該ArrayList由定義初始化作爲一個字段,在這裏:

private List<Object> allData = new ArrayList<Object>(); 

但你似乎在構造函數在這裏設置非常相同的ALLDATA爲null:

public CoursesSupportBean() { 
    url=""; 
    DataSourceName=""; 
    error=""; 
    allData = null; 
    course_name=""; 
    courseId=0; 
    obj=null; 
} 

所以你得到一個空指針異常,因爲你設置的ArrayList參考ALLDATA已經被初始化再次空(不再指向所創建的ArrayList)

只是刪除這一行:

allData = null; 

通常情況下,您初始化在構造函數中的所有成員,但共同的初始化選擇通常留在字段定義中,除非它是一個昂貴的操作,因此儘可能延遲。

3

您設置allDataArrayListnull在構造函數中。刪除此行:

allData = null; 

從那裏。通常,如果要在構造函數中初始化變量,只需在構造函數外部的類中聲明它們,並僅在構造函數中初始化它們。換言之,代替上面allData = null;符合:

allData = new ArrayList<Object>(); 

替換構造的外這一行:

private List<Object> allData = new ArrayList<Object>(); 

與此:

private List<Object> allData;