简体   繁体   中英

call public String from other class

I try to get string with time (hh:mm:ss) but i get this error:

     10-28 12:35:27.682: E/AndroidRuntime(14002): java.lang.NullPointerException

I call to this by my main class:

private TimeUpdater timeupdater;
.
.
.
private Runnable runnable = new Runnable(){ 

    public void run() {

        datasource = new DataSource(getBaseContext());
        datasource.open();

        comment = datasource.getComment("1");
        data = comment.toString().split(" ");



        /*for(int i=0;i<11;i++)
        {

            dataUpdate[i]=data[i+1];
            System.out.println("U " + dataUpdate[i]);
            System.out.println("D " + data[i+1]);
        }*/

        String a = timeupdater.upTime(dane[5]);

        System.out.println(a);

        time.setText(timeupdater.upTime(dane[5]));
        //for(int i=0;i<12;i++)
            //System.out.println(dane[i]);



        handler.postDelayed(this, 1000);
    }
};

inside data[5] is 00:00:00
This is my Time Updater class:

public class TimeUpdater {

private int hh, mm, ss;
private String time;
private String shh, smm, sss;
public String upTime(String time) {

    String[] czas = time.split(":");
    hh = Integer.parseInt(czas[0]);
    mm = Integer.parseInt(czas[1]);
    ss = Integer.parseInt(czas[2]);
    if(ss<60)
        ss++;
    if(ss==60)
    {
        ss=0;
        mm++;
    }
    if(mm==60)
        hh++;
    if(hh<10)
        shh = "0"+hh;
    if(mm<10)
        smm = "0"+mm;
    if(ss<10)
        sss = "0"+ss;
    this.time = shh + ":" + smm + ":" + sss;
    System.out.println(time);
    return this.time;
}

I need because later i want to do more that calls. I need to know how to create in other class public Strings to change data every second

While calling the method upTime , you need to instantiate its parameter( time ), before calling this method.

This is the point where exception occurs

String[] czas = time.split(":");

Now since time is not instantiated, you are getting the NullPointerException

Try following way,

String time = timeupdater.upTime(data[5].toString());

Your method upTime() requires a String parameter to be passed. Your data[] array may be of String type but for error prevention it is advisable to use .toString() method with it.

I fixed my problem by add

 timeupdater = new TimeUpdater();

before

 String a = timeupdater.upTime(dane[5]);

thanks for try to help me

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