0

This code used to work fine. I didnt change anything on it. And since ive made my class a fragment and insert swipe to another fragment it doesnt work.

i get the exception on this 'if':

    if (checkInput(tvWaybillDate.getText().toString(), etFlightNo.getText().toString(), etLitresObserved.getText().toString(), etTempObserved.getText().toString(), etDensity
                        .getText().toString(), strAirlinesId, strAirportsId, tvBouzer.getText().toString(), strLocationID, tvVCF.getText().toString(), tvLitres.getText().toString(), tvMT
                        .getText().toString(), tvA15.getText().toString()) == 13) {
.............
}

My fields here are indeed null or some "". But i check them in checkInput where the values are going.

checkInput:

private int checkInput(String waybillDate, String flightNo, String litresObserved, String tempObserved, String density, String airlineId, String airportId, String bouzer,
        String locationId, String vcf, String litres, String mt, String a15) {
    int count = 0;
    valueList = new ArrayList<String>();

    if (!waybillDate.equals("")) {
        count++;
    } else {
        valueList.add("Waybill Date");
    }

    if (!flightNo.equals("") && !flightNo.equals("0") && flightNo != null) {
        count++;
    } else {
        valueList.add("Flight No");
    }

    if (!litresObserved.equals("") && !litresObserved.equals("0") && litresObserved != null) {
        count++;
    } else {
        valueList.add("Litres Observed");
    }

    if (!tempObserved.equals("") && !tempObserved.equals("0") && tempObserved != null) {
        count++;
        } else {
            valueList.add("Temp Observed");
        }
    .
    .
    .
    .
    .
    .}

And here are the exceptions i get:

05-29 17:11:21.063: E/AndroidRuntime(10039): FATAL EXCEPTION: AsyncTask #3
05-29 17:11:21.063: E/AndroidRuntime(10039): java.lang.RuntimeException: An error occured while executing doInBackground()
05-29 17:11:21.063: E/AndroidRuntime(10039):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at java.lang.Thread.run(Thread.java:838)
05-29 17:11:21.063: E/AndroidRuntime(10039): Caused by: java.lang.NullPointerException
05-29 17:11:21.063: E/AndroidRuntime(10039):    at petrolina.pptaviation.NewWaybillPage1.sendWaybill(NewWaybillPage1.java:468)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at petrolina.pptaviation.NewWaybillPage1.access$28(NewWaybillPage1.java:465)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at petrolina.pptaviation.NewWaybillPage1$AsyncSend.doInBackground(NewWaybillPage1.java:1717)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at petrolina.pptaviation.NewWaybillPage1$AsyncSend.doInBackground(NewWaybillPage1.java:1)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-29 17:11:21.063: E/AndroidRuntime(10039):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-29 17:11:21.063: E/AndroidRuntime(10039):    ... 4 more

Any ideas whats wrong?

  • Not exactly duplicate but helpful: http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate - in the comments to an answer you say you find the view references in `onCreate()` which is the wrong place / too early for a fragment. – laalto May 29 '14 at 14:27
  • where should i initialize them then? – Antonis Lambrianides May 29 '14 at 14:41
  • Fragment's `onCreateView()` after inflating the layout, calling `findViewById()` on the inflated layout. – laalto May 29 '14 at 14:44
  • yes thats what i have srr i wasnt specific. It doesnt work as onCreate in a fragment anw. They are initialized in onCreateView – Antonis Lambrianides May 30 '14 at 04:44

2 Answers2

0

Are those views like tvWaybillDate initialized before if statement?

something like:

TextView tvWaybillDate = (TextView) findViewById(R.id.xxx);

where xxx is id from layout.

Also, in your function checkInput you should check if Strings are null before checking if they are empty. So, instead of:

if (!waybillDate.equals(""))

you should write

if (waybillDate != null && !waybillDate.equals(""))

for every string.

Zoran
  • 1,484
  • 1
  • 10
  • 13
0

As far as why a TextView might be null if you "set" them in onCreate(), you may be trying to set them before the view itself is actually visible. You need to work with views when you are certain they are drawn in your view. For that, move your view sets to onStart, that is:

@Override
public void onStart(){
     super.onStart();
     tvWaybillDate.setText("some text");
     ...
}

This is based on the lifecycle of your activity http://developer.android.com/training/basics/activity-lifecycle/starting.html or fragment http://developer.android.com/guide/components/fragments.html

It is fine to try to pass a null as a parameter, but to try to convert a null to an object or a primitive is where the error is coming from. You need pass your views themselves, or the text character sequences they contain, to your method. For example:

 tvWaybillDate.getText().toString(); 

will throw a NPE if tvWaybillDate is null; you can't convert a null to a string. But passing

 checkInput(tvWaybillDate, ...

 ...

 public void checkInput(TextView tvWaybillDate, ...){
     int count = 0;
     if(tvWaybillDate != null) count++;
     ...
     return count;
 }

Do that, or something similar like passing the CharacterSequence tvWaybillDate.getText() for all of your views and you should be fine.

zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • This prevents the NPE but doesn't solve the root problem *why* they were null in the first place. – laalto May 29 '14 at 14:27
  • Ill give this a try. But i dont think it will fix the issue. It used to work. Does the fragment actually effects this? – Antonis Lambrianides May 29 '14 at 14:41
  • @Antonis Lambriandes A millisecond makes all the difference in programming. It may take a bit longer to load in your fragment so your views haven't had time to load. I can almost assure you moving your set's to onStart will solve your problem. As laalto said you can move it to onCreateView() but even then I am pretty sure you are running a chance of having undrawn views, onStart() guarantees your views are there. – zgc7009 May 29 '14 at 14:46
  • They are in OncreateView. I forgot and said onCreate instead of onCreateView. Ill try moving them to onStart and see what happens – Antonis Lambrianides May 30 '14 at 04:48
  • I found the problem m8. There were some values in my if that i was calling them from another class and they were also null but those were the problem, not the rest. It was a newly added line and it sliped from me:P Anw thank you for the suggestions – Antonis Lambrianides Jun 02 '14 at 05:39