简体   繁体   中英

function return resultset, get in another class - JAVA

I am new in Java. I am doing this thing it creates error, please let me know why I am going in this function dispatchUncaughtException . Am I doing right ? If I am not so how can this thing possible ?

This is dataCollection Class

public class dataCollection {

    private ResultSet my_rs1 =  null;

    public ResultSet mydata()
    {
        try
        {
            String get_data_query = "SELECT * FROM my_table ";
            my_rs1 = cn.stmt.executeQuery(aggregator_data_query);

        } catch(SQLException SQLex)
        {
            System.out.println("SQL Error: " + SQLex.getMessage());
        }

        return my_rs1;        

    }

}

This is callingClass // where we call ResultSet

public class callingClass
{
    dataCollection dc = new dataCollection;
    private ResultSet my_call_data = dc.mydata();
    while(my_call_data.next())
    {
        //Code Here
    }
}

EDIT

Exception in thread "Thread-2" java.lang.NullPointerException
    at sms_sender.dataCollection.aggregator_data(dataCollection.java:29)
    at sms_sender.threadClass.run(threadClass.java:27)

correct your parameter :

String get_data_query = "SELECT * FROM my_table";
my_rs1 = cn.stmt.executeQuery(get_data_query);

Edit: Step 2 => Check your db connection its working properly. i think your stmt is empty or incorrect.

code should be in function or block in java.

    public class callingClass
    {
        dataCollection dc = new dataCollection;
        private ResultSet my_call_data;

        public void showData(){
        my_call_data = dc.mydata();
         while(my_call_data.next()){
            //Code Here
         }
       }

    }

What is happening is that you are not catching an Exception so the Thread.UncaughtExceptionHandler will try to forward this Exception.

First try to catch the Exception and give us the stacktrace :

catch (Exception e) {
  e.printStackTrace(); // if you are not using a logger like log4j just print it.
}

Then i would recommend that you read more about Exceptions

And finally, as a notice class names should begin by a Capital letter see Naming Conventions .

Good reading :)

PS : i did not notice that your code was not in a method / function, but i suppose this is just a bad edit as the class would not even compile.

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