简体   繁体   中英

NullPointerException in calling a Method in java

I have a method in my project given below:

public String fillsubject(String id,int type){
    if(id.isEmpty())
        return "";
    String result="";
    String query="select sub_name from subject where sub_id='"+id+"'";
    if(type==1)
        query="select lab_name from lab where lab_id='"+id+"'";
    try {
        ResultSet rs2=st2.executeQuery(query);
        rs2.first();
        result=rs2.getString(1);
        rs2.close();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, ex);
    }
    return result;
}

I am calling it as written below..

    try {
        rs = st.executeQuery("Select sub1,sub2,sub3,sub4,sub5,sub6,sub7,lab1,lab2,lab3,class_id from class where standard='" + jComboBox32.getSelectedItem().toString() + "' and section='" + jComboBox26.getSelectedItem().toString() + "';");
        rs.first();
        jTextField39.setText(fillsubject(rs.getString(1),0));
        jTextField32.setText(fillsubject(rs.getString(2),0));
        jTextField33.setText(fillsubject(rs.getString(3),0));
        jTextField40.setText(fillsubject(rs.getString(4),0));
        jTextField35.setText(fillsubject(rs.getString(5),0));
        jTextField41.setText(fillsubject(rs.getString(6),0));
        jTextField38.setText(fillsubject(rs.getString(7),0));
        jTextField37.setText(fillsubject(rs.getString(8),1));
        jTextField34.setText(fillsubject(rs.getString(9),1));
        jTextField36.setText(fillsubject(rs.getString(10),1));
        jLabel198.setText(rs.getString(11));
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(DeleteStudent, e);
    } 

the problem is when calling rs.getString(), it sometimes have null value in database. In this situation i get an nullpointer exception in fillsubject() method. now my question is how do i tackle with this problem. Thanks in advance.

in your method fillsubject() , instead of using:

if (id.isEmpty())

use:

if (id == null || id.isEmpty())

by that, you will check if the id String object is null before trying to access it.

Also, just to add information, in the suggested if clause, id.isEmpty() will be executed ONLY if the first comparison results false (only if id is not null). When id is null, id.isEmpty() is never going to be executed.

At the beginning of your method, add:

if (id == null)
    return ""; // or whatever you want to represent no value

You can always check if the String returned by getString is null yourself.

if (result == null) { // handle it! }

In general, you can use the wasNull() method to check if the last "get" method represented a null . This becomes necessary when getting other datatypes such as int that don't have a representation for null .

Reports whether the last column read had a value of SQL NULL. Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.

if (rs.wasNull()) {
    // previous "get" method represents a DB null.
}

I believe that the best way to handle this is to run an if statement on the result set and then handle the result as you see fit for your application. EX:

if(rs.wasNull()){
// Handle the result } 
else { 
//handle the rest of your code
}

I hope that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM