Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.delete() is ignored [duplicate]

Tags:

java

android

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.

like image 807
bo2 Avatar asked Nov 08 '22 21:11

bo2


1 Answers

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])

like image 114
yole Avatar answered Nov 14 '22 23:11

yole