java.lang.NullPointerException when made a required change to the code (Java Netbeans IDE 7.4 & JDK 1.7)

huangapple 未分类评论46阅读模式
标题翻译

java.lang.NullPointerException when made a required change to the code (Java Netbeans IDE 7.4 & JDK 1.7)

问题

以下是您提供的代码的翻译部分:

public class JFrameAllMarks extends javax.swing.JFrame {

    public JFrameAllMarks() {
        initComponents();
        setDefaultCloseOperation(JFrameAllMarks.DISPOSE_ON_CLOSE);
        showMark();
    }

    public ArrayList<Marks> markList1() {
        ArrayList<Marks> marksList = new ArrayList<>();
        try {
            DBConnection obj = new DBConnection();
            String selectquery = "SELECT * FROM Marks";
            ResultSet rs = obj.getdata(selectquery);
            Marks objmk;
            while(rs.next()) {
                objmk = new Marks(rs.getString("StudentID"), rs.getString("StudentName"), rs.getString("SubjectID"), rs.getString("SubjectName"), rs.getString("BatchID"), rs.getInt("Marks"));
                marksList.add(objmk);
            }
        } catch (SQLException ex) {
            Logger.getLogger(JFrameAllMarks.class.getName()).log(Level.SEVERE, null, ex);
        }
        return marksList;
    }

    public void showMark() {
        ArrayList<Marks> list = markList1();
        DefaultTableModel dtm = (DefaultTableModel)jTableAllMarks.getModel();
        Object[] row = new Object[6];
        for(int i=0; i<list.size(); i++) {
            row[0] = list.get(i).getStudentID();
            row[1] = list.get(i).getStudentName();
            row[2] = list.get(i).getSubjectID();
            row[3] = list.get(i).getSubjectName();
            row[4] = list.get(i).getBatchID();
            row[5] = list.get(i).getMarks();
            dtm.addRow(row);
        }
    }
}

class Marks {
    private String StudentID, StudentName, SubjectID, SubjectName, BatchID;
    private int Marks;

    public Marks(String StudentID, String StudentName, String SubjectID, String SubjectName, String BatchID, int Marks) {
        this.StudentID = StudentID;
        this.StudentName = StudentName;
        this.SubjectID = SubjectID;
        this.SubjectName = SubjectName;
        this.BatchID = BatchID;
        this.Marks = Marks;
    }

    public String getStudentID() {
        return StudentID;
    }

    public String getStudentName() {
        return StudentName;
    }

    public String getSubjectID() {
        return SubjectID;
    }

    public String getSubjectName() {
        return SubjectName;
    }

    public String getBatchID() {
        return BatchID;
    }

    public int getMarks() {
        return Marks;
    }
}

请注意,我只提供了代码的翻译部分,如有任何问题或需要进一步的帮助,请随时提问。

英文翻译

My initial piece of code is the following.

public class JFrameAllMarks extends javax.swing.JFrame {
    
    
        public JFrameAllMarks() {
        initComponents();
        setDefaultCloseOperation(JFrameAllMarks.DISPOSE_ON_CLOSE);
        showMark();
        
    }

 public ArrayList&lt;Marks&gt; markList1()
        {
            ArrayList&lt;Marks&gt; marksList = new ArrayList&lt;&gt;();
                
             try {   
                DBConnection obj = new DBConnection();
                
                String selectquery = &quot;SELECT * FROM Marks&quot;;
                
                ResultSet rs = obj.getdata(selectquery);
                
                Marks objmk;
                
                while(rs.next())
                {
                    objmk = new Marks(rs.getString(&quot;StudentID&quot;), rs.getString(&quot;StudentName&quot;), rs.getString(&quot;SubjectID&quot;), rs.getString(&quot;SubjectName&quot;), rs.getString(&quot;BatchID&quot;), rs.getInt(&quot;Marks&quot;));
                    marksList.add(objmk);
                }
            } 
            catch (SQLException ex) {
                Logger.getLogger(JFrameAllMarks.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return marksList;
        }
 
 
 public void showMark()
 {
     ArrayList&lt;Marks&gt; list = markList1();
     
     DefaultTableModel dtm = (DefaultTableModel)jTableAllMarks.getModel();
     
     Object[] row = new Object[6];
     
     for(int i=0; i&lt;list.size();i++)
     {
         row[0] = list.get(i).getStudentID();
         row[1] = list.get(i).getStudentName();
         row[2] = list.get(i).getSubjectID();
         row[3] = list.get(i).getSubjectName();
         row[4] = list.get(i).getBatchID();
         row[5] = list.get(i).getMarks();
         
         dtm.addRow(row);
         
     }
 }

And the required java class for the ArrayList in the above code.

package AllMarksList;

/**
 *
 * @author Admin
 */
class Marks {
    private String StudentID, StudentName, SubjectID, SubjectName, BatchID;
    private int Marks;
    
    public Marks (String StudentID, String StudentName, String SubjectID, String SubjectName, String BatchID, int Marks)
    {
        this.StudentID=StudentID;
        this.StudentName=StudentName;
        this.SubjectID=SubjectID;
        this.SubjectName=SubjectName;
        this.BatchID=BatchID;
        this.Marks=Marks;
       
     }

    
    public String getStudentID()
    {
        return StudentID;
    }
    
    public String getStudentName()
    {
        return StudentName;
    }
    
    public String getSubjectID()
    {
        return SubjectID;
    }
    
    public String getSubjectName()
    {
        return SubjectName;
    }
    
    public String getBatchID()
    {
        return BatchID;
    }
    
    public int getMarks()
    {
        return Marks;
    }
       
}

The above code executed correctly. It showed all data that resided in the "Marks" database table.

So, I copied and pasted this and the relevant class to create another JFrame and java class that functions the exact same way. Relevant changes were made to have distinct changes between the original and the copied JFrames and java classes.

In this new JFrame, I wanted to extract from the database, only the data that contained a certain Student ID.
So, I changed the select query to the following.

String selectquery = &quot;SELECT * FROM Marks WHERE StudentID=S001&quot;;

And when executing this new code, I received the following errors.

SEVERE: null
java.lang.NullPointerException
	at StudentMarks.JFrameStudentAllMarks.markList1(JFrameStudentAllMarks.java:47)
	at StudentMarks.JFrameStudentAllMarks.showMark1(JFrameStudentAllMarks.java:63)
	at StudentMarks.JFrameStudentAllMarks.&lt;init&gt;(JFrameStudentAllMarks.java:30)
	at StudentMarks.JFrameStudentAllMarks$3.run(JFrameStudentAllMarks.java:224)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
	at java.awt.EventQueue.access$300(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:706)
	at java.awt.EventQueue$3.run(EventQueue.java:704)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Tried making changes to the lines of code, pinpointed by the error.
But had no luck in solving this.

Would really appreciate your assistance.

答案1

得分: 0

因为您是将SQL查询创建为Java字符串,所以您需要使用SQL语法。SQL使用单引号来界定字符串,因此您需要手动将其添加到您的查询中。

这是所需的字符串:

String selectquery = "SELECT * FROM Marks WHERE StudentID='S001'";
英文翻译

Because you created the SQL query out of a Java String, you need to use SQL syntax. SQL uses single-quotes to delimit a string, so you need to add those into your query manually.

This is the string that is needed:

String selectquery = &quot;SELECT * FROM Marks WHERE StudentID=&#39;S001&#39;&quot;;

huangapple
  • 本文由 发表于 2020年3月16日 22:50:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/60708143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定