2014-01-22 94 views
-2
public class Model 
{ 
    public static Connection getConnection() 
    { 
     Connection conn = null; 
    try 
    { 
     Class.forName("oracle.jdbc.OracleDriver"); 
     conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",  "System", "system"); 
    } 
    catch(ClassNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
    catch(SQLException e) 
    { 
     e.printStackTrace(); 
    } 
    return conn; 
} 

public static class Cart 
{ 
    public String itmName=""; 
    public int howmany=0; 
    public static long itmQty=0, itmID=0; 
    public double itmPrice=0.0, itmCost=0.0, totalSum=0.0; 
} 

public static ArrayList<Cart> getCartDatabase(String user) throws Exception 
{ 
    Connection conn = getConnection(); 
    String sql = "select * from userCarts where userID = '" + user + "'"; 
    PreparedStatement pstmt = conn.prepareStatement(sql); 
    ResultSet rst = pstmt.executeQuery(); 
    ArrayList<Cart> al = null; 
    Cart crt=null; 
    while(rst.next()) 
    { 
     System.out.println("CPoint"); 
     try 
     { 
      long p = rst.getLong("itemID"); 
      crt.itmID = p; // This is the line thats creating the error 
      System.out.println(p + " is long! I guess..."); 
     } 
     catch(NullPointerException e) 
     { 
      System.out.println("NPE Caught in Model"); 
     } 
     System.out.println("CP 1 " + crt.itmID); 
     ArrayList<row> alr=null; 
     try 
     { 
      alr = Model.getStoreInventory(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     System.out.println("CP 2"); 
     for(int i=0; i<alr.size(); i++) 
     { 
      crt.itmName = alr.get(i).itmName; 
      crt.itmPrice = alr.get(i).itmPrice; 
      crt.itmQty = alr.get(i).itmQty; 
     } 
     System.out.println("CP 3"); 
     crt.howmany = rst.getInt("howmany"); 
     crt.itmCost = crt.itmPrice*crt.howmany; 
     al.add(crt); 
    } 
    return al; 
    } 
} 

当我尝试访问的getCartFromDatabase这种方法,它提供了NullPointerException但我不明白为什么它会做到这一点。此外,我试图使类作为一个非静态类太多,但它仍然给了同样的错误:Deferencing空指针

"Possible deferencing Null Pointer"

+4

第一线,你可以发布堆栈跟踪?你在哪一行获得NPE? –

+1

在运行时你会得到一个真正的'NullPointerException'吗?还是你在编译时得到'可能的引用空指针'警告? java编译器通常不会发出这样的警告,您是使用FindBugs还是类似的工具? –

+0

除非您有充分的理由这么做,否则绝对不要在您的代码中捕获'NPE'。只有在以任何方式使异常可见时才捕获任何'RuntimeException',否则您无法知道发生了什么。 – skiwi

回答

2
Cart crt=null; 
while(rst.next()) 
{ 
    System.out.println("CPoint"); 
    try 
    { 
     long p = rst.getLong("itemID"); 
     crt.itmID = p; // This is the line thats creating the error 
     System.out.println(p + " is long! I guess..."); 
    } 

crtnull当您尝试访问crt.itemID。您必须先为其分配一个实例。

我想你可以简单地改变从片段到

Cart crt = new Cart(); 
+0

这帮助了很多......但现在有线al.add(crt)的错误 CPoint 1234567890很长!我猜... CP 1 1234567890 CP 2 CP 3 异常线程 “main” 显示java.lang.NullPointerException \t在com.Model.getCartDatabase(Model.java:343) \t在com.TEST.main (TEST.java:34) Java结果:1 – hillyolsen

+0

尝试使用调试器对其进行处理。我相信你会找到“坏男孩”...... – Fildor

+0

谢谢! ArrayList也没有初始化。现在初始化它并得到它....真的很感激它! – hillyolsen