验证码中的空指针异常

时间:2017-08-04 10:56:31

标签: android nullpointerexception captcha

我正在以我的形式实现验证码,但是我得到方法getImage()的空指针异常。

我在项目中有两个验证码,一个工作正常,但另一个显示以下错误。我不知道为什么它没有将验证码作为参数。

我在下面提供了logcat和mainactivity.java。

logcat的

FATAL EXCEPTION: main

Process: com.mws.tms_application, PID: 6983                                                                  
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mws.tms_application/com.mws.tms_application.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.mws.tms_application.TextCaptcha.getImage()' on a null object reference
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
     at android.app.ActivityThread.-wrap11(Unknown Source:0)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
     at android.os.Handler.dispatchMessage(Handler.java:105)
     at android.os.Looper.loop(Looper.java:164)
     at android.app.ActivityThread.main(ActivityThread.java:6540)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.mws.tms_application.TextCaptcha.getImage()' on a null object reference
     at com.mws.tms_application.MainActivity.onCreate(MainActivity.java:41)
     at android.app.Activity.performCreate(Activity.java:6980)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
     at android.app.ActivityThread.-wrap11(Unknown Source:0) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
     at android.os.Handler.dispatchMessage(Handler.java:105) 
     at android.os.Looper.loop(Looper.java:164) 
     at android.app.ActivityThread.main(ActivityThread.java:6540) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

main activity.java

package com.mws.tms_application;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
     TextView Registerlink;
     Button Submit_btn ,Reset_btn,Link_btn;
     EditText Username,Usermob,UserAddress,Usermailid,Userpass,Usercapt;
     ActionBar actionBar;
     ImageView imageView1;
     TextCaptcha textCaptcha1;
     //#2eb82e
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Intent intent = getIntent();
          Registerlink = (TextView) findViewById(R.id.loginlink);
          Submit_btn=(Button)findViewById(R.id.Submit_btn);
          Reset_btn=(Button)findViewById(R.id.Main_Reset_btn);
          Username=(EditText)findViewById(R.id.username_edtext);
          Usermob=(EditText)findViewById(R.id.usermob_no_edtext);
          UserAddress=(EditText)findViewById(R.id.userAddresss_edtext);
          Usermailid=(EditText)findViewById(R.id.usermailid_edtext);
          Userpass=(EditText)findViewById(R.id.userpass_edtext);
          Usercapt=(EditText)findViewById(R.id.usercapt_edtext);
          Link_btn=(Button)findViewById(R.id.Link_btn);
          imageView1=(ImageView)findViewById(R.id.register_capt_imageview);
          imageView1.setImageBitmap(textCaptcha1.getImage());
          actionBar=getSupportActionBar();
          actionBar.show();
          actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2eb82e")));

          MainLogic();
     }

     private void MainLogic()
     {    Link_btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
               Intent i=new Intent(MainActivity.this,Home_Navigation_Activity.class);
               startActivity(i);
          }
     });
          Submit_btn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                    isValidData();
               }
          });
          Reset_btn.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
               claredata();
               }
          });
          Registerlink.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                    Intent i=new Intent(MainActivity.this,Login_Activity.class);
                    startActivity(i);
               }
          });
     }
     /* Userid,Username,usermob,userAddress,usermailid,userpass,usercapt;*/

     public  boolean isValidData()
     {
        String id,name,mobile,address,email,password,captch;

          name=Username.getText().toString();
          mobile=Usermob.getText().toString();
          address=UserAddress.getText().toString();
          email=Usermailid.getText().toString();
          password=Userpass.getText().toString();
          /*captch=Usercapt.getText().toString();*/
          if (!name.equals("")&&!mobile.equals("")&&mobile.length()>=10&&mobile.length()<=13&&!address.equals("")&&!email.equals("")&&!password.equals(""))
          {
               if (validEmail((email)))
               {
                    return true;
               }
               else
               {
                    AlertDialog.Builder alt=new AlertDialog.Builder(MainActivity.this);
                    alt.setMessage("Invalid Email_Id");
                    alt.setCancelable(true);
                    alt.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                              dialog.dismiss();
                         }
                    });
                    AlertDialog alertDialog=alt.create();
                    alertDialog.show();
                    return false;
               }

          }
          else
          {
          AlertDialog.Builder alt=new AlertDialog.Builder(MainActivity.this);
               alt.setMessage("Please fill all details");
               alt.setCancelable(true);
               alt.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                    }
               });
               AlertDialog alertDialog=alt.create();
               alertDialog.show();
          }
        return false;
     }
     public boolean claredata()
     {
          Username.setText("");
          Usermob.setText("");
          UserAddress.setText("");
          Usermailid.setText("");
          Userpass.setText("");
          /*captch=Usercapt.getText().toString();*/
          return false;
     }
     private boolean validEmail(String email) {
          final String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
          if (email.trim().matches(emailPattern))
          {
               return true;
          }
          return false;

     }

}

1 个答案:

答案 0 :(得分:1)

来自

imageView1.setImageBitmap(textCaptcha1.getImage());

您正在使用textCaptcha1,但尚未将其初始化