简体   繁体   English

在java中调用Method时出现NullPointerException

[英]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. 问题是当调用rs.getString()时,它有时在数据库中有空值。 In this situation i get an nullpointer exception in fillsubject() method. 在这种情况下,我在fillsubject()方法中得到一个nullpointer异常。 now my question is how do i tackle with this problem. 现在我的问题是如何解决这个问题。 Thanks in advance. 提前致谢。

in your method fillsubject() , instead of using: 在你的方法fillsubject() ,而不是使用:

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. 通过这种方式,您将在尝试访问之前检查id String对象是否为null

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). 另外,只是为了添加信息,在建议的if子句中,如果第一个比较结果为false(仅当id不为null),则仅执行id.isEmpty() )。 When id is null, id.isEmpty() is never going to be executed. 当id为null时,永远不会执行id.isEmpty()

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. 您始终可以检查getString返回的String是否为null

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

In general, you can use the wasNull() method to check if the last "get" method represented a null . 通常,您可以使用wasNull()方法检查最后一个“get”方法是否表示为null This becomes necessary when getting other datatypes such as int that don't have a representation for null . 当获取没有null表示的其他数据类型(如int ,这是必要的。

Reports whether the last column read had a value of SQL NULL. 报告最后一列读取的值是否为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. 请注意,您必须首先调用列上的其中一个getter方法以尝试读取其值,然后调用方法wasNull以查看读取的值是否为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. 我认为处理此问题的最佳方法是在结果集上运行if语句,然后根据您的应用程序处理结果。 EX: EX:

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

I hope that makes sense. 我希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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