5

I'm currently having some error which gave me null pointer. So here's the code.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.catalog_view);
    name = (TextView) findViewById(R.id.textView1);
    buttonDel = (Button) findViewById(R.id.buttonDel);
    buttonBack = (ImageButton) findViewById(R.id.imageButton1);


    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

    Bundle bundle = getIntent().getExtras();
    mPath = bundle.getString("path");

    thelabels = new labels(mPath);
    thelabels.Read();

    count = 0;
    int max = thelabels.max();
    for (int i = 0; i <= max; i++)
    {
        if (thelabels.get(i) != "") {
            count++;
        }
    }

    bmlist = new Bitmap[count];
    namelist = new String[count];
    count = 0;
    for (int i = 0; i < max; i++){
        if (thelabels.get(i) != "");{
            File root = new File(mPath);
            final String fname = thelabels.get(i);
            FilenameFilter pngFilter = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().startsWith(fname.toLowerCase() + "-");
                }
            };

            File[] imageFiles = root.listFiles(pngFilter);
            if (imageFiles.length > 0){
                InputStream is;
                try{
                    is = new FileInputStream(imageFiles[0]);

                    bmlist[count] = BitmapFactory.decodeStream(is);
                    namelist[count] = thelabels.get(i);
                }
                catch (FileNotFoundException e){
                    // TODO Auto-generated catch block
                    Log.e("File error", e.getMessage() + " " + e.getCause());
                    e.printStackTrace();
                }
            }

            count++;
        }
    }
    g = (Gallery) findViewById(R.id.gallery1);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemSelectedListener(this);


    buttonBack.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    });

    buttonDel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            File root = new File(mPath);
            FilenameFilter pngFilter = new FilenameFilter() {
                public boolean accept(File dir, String n) {
                    String s = name.getText().toString();
                    return n.toLowerCase().startsWith(s.toLowerCase() + "-");
                }
            };
            File[] imageFiles = root.listFiles(pngFilter);
            for (File image : imageFiles) {
                image.delete();
                int i;
                for (i = 0; i < count; i++) {
                    if (namelist[i].equalsIgnoreCase(name.getText().toString())) {
                        Log.i("Delete operation", name.getText().toString());
                        int j;
                        for (j = i; j < count - 1; j++) {
                            namelist[j] = namelist[j + 1];
                            bmlist[j] = bmlist[j + 1];
                        }
                        count--;
                        refresh();
                        break;
                    }
                }
            }
        }
    });
}

The error was point to this line

if (namelist[i].equalsIgnoreCase(name.getText().toString()))

The error

 Process: com.example.syafiq.facialrecognition, PID: 29294
                                                                                  java.lang.NullPointerException
                                                                                      at com.example.syafiq.facialrecognition.ImageGallery$3.onClick(ImageGallery.java:135)
                                                                                      at android.view.View.performClick(View.java:4654)
                                                                                      at android.view.View$PerformClick.run(View.java:19438)
                                                                                      at android.os.Handler.handleCallback(Handler.java:733)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:146)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5602)
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                                      at dalvik.system.NativeStart.main(Native Method)

I'm really sorry if my question is not well prepared. I'm new to android programming. I really hope you guys can help me solve this error :( I really appreciate your time.

bo2
  • 653
  • 1
  • 7
  • 12
  • 2
    Did you look at the return value from `delete`? My guess is that it's returning `false`... – Jon Skeet Jan 26 '16 at 08:23
  • 1
    What error did you get? Please post your `LogCat` output. – Ken Y-N Jan 26 '16 at 08:25
  • 3
    What is `namelist`, and why do you think the exception has anything to do with the `delete` call? – yole Jan 26 '16 at 08:25
  • as pointed out by @yole you are most likely not initializing the `namelist` array or the `name` variable, if this is the cause of exception as you said. – SomeJavaGuy Jan 26 '16 at 08:27
  • wait guys, hold on im editing the question – bo2 Jan 26 '16 at 08:29
  • 1
    name.getText() is used before and did not fail so it is not null. namelist is null or any array position of it is null. – aalku Jan 26 '16 at 08:29
  • image.delete() will return true if success or false if it can't delete it for some reason. – aalku Jan 26 '16 at 08:30
  • edited. I'm really sorry for my lack explanation in my question – bo2 Jan 26 '16 at 08:33
  • `The error was point to this line` - If you have an error - you need to post the stacktrace. That is very important. – Knossos Jan 26 '16 at 08:37
  • 2
    You definitely should stop reusing variables for different things. It is very hard to follow what 'count' means where. One variable should mean only one thing to be easy to mantain. You might make a mistake and the variable would keep a previous value from the previous use. – aalku Jan 26 '16 at 08:39
  • edited with error logcat – bo2 Jan 26 '16 at 08:42
  • It isn't ignored, but it can fail. However null pointer exceptions have exactly nothing to do with file deletion. Please format this illegible mess. – user207421 Nov 11 '19 at 22:35

1 Answers1

1

Your namelist array will contain null elements. The code initializing elements in namelist is executed only under the condition if (imageFiles.length > 0). This is what causes the exception.

You can avoid it by swapping the arguments of the call: name.getText().toString().equalsIgnoreCase(namelist[i])

yole
  • 92,896
  • 20
  • 260
  • 197