This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


1年前关闭。




我目前遇到一些错误,这使我得到了空指针。所以这是代码。
@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;
                    }
                }
            }
        }
    });
}

错误是指向这一行
if (namelist[i].equalsIgnoreCase(name.getText().toString()))

错误
 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)

如果我的问题准备得不好,我真的很抱歉。我是android编程的新手。我真的希望你们能帮助我解决这个错误:(非常感谢您的宝贵时间。

最佳答案

您的namelist数组将包含空元素。 namelist中的代码初始化元素仅在if (imageFiles.length > 0)条件下执行。这是导致异常的原因。

您可以通过交换调用参数来避免它:name.getText().toString().equalsIgnoreCase(namelist[i])

10-08 13:15