1

How can I safely read both of my array values from another class method without the nullexception warning coming up.I am getting both values orreclty just worried about the "Array access x[0] may produce NPE " warning without using intents

 public Class ImageUtility{

 public static String[] savePicture(Context context, Bitmap bitmap) {

  ......

   String[] arr = new String[2];
        arr[0] = img_name;
        arr[1] = img_path;

        return arr;
   }



public Class Others{

 public void Test(){
 String[] x = ImageUtility.savePicture(getActivity(), bitmap);
 value_one= x[0]; //nullexception warning is here
 value_two= x[1];

  }
7
  • check the length of x, if it is 0, there is something wrong with your method. Commented Jul 13, 2016 at 4:48
  • 1
    why would value_one= x[0]; give you a NPE even if x[0] is null? Commented Jul 13, 2016 at 4:48
  • how you get img_name and img_path? Commented Jul 13, 2016 at 4:49
  • @Cgx they are strings am collecting nothing to worry about there..both are not null Commented Jul 13, 2016 at 4:54
  • @Aeonia maybe it's a AsyncTask when you savePicture,so when you call test(), the savePicture not finished yet Commented Jul 13, 2016 at 4:57

1 Answer 1

1

Try the following code

 public void Test(){
   try{
     String[] x = ImageUtility.savePicture(getActivity(), bitmap);
     if(x != null && x.Length >= 1){
       value_one= x[0]; 
       value_two= x[1];
     }
   }catch(NullPointerException e){
     //print log
   }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

am now getting the warning Deference of x may produce NPE right at x.length
if(x != null && x.Length >= 1){
Thanks..warning is no longer present

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.