Sofari Agali

Sofari Agali

  • 1.5k
  • 276
  • 26.1k

Null pointer exception when trying to store

Oct 11 2017 5:48 AM

I'm getting Null pointer exception when trying to upload image and store edittext 
 
my log cat below
 
10-11 10:29:12.908 2396-2396/com.example.sofari.tiffanezni E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sofari.tiffanezni, PID: 2396
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.example.sofari.tiffanezni.Register.profileImage(Register.java:146)
at com.example.sofari.tiffanezni.Register.getValues(Register.java:160)
at com.example.sofari.tiffanezni.Register.addContact(Register.java:166)
at com.example.sofari.tiffanezni.Register.access$000(Register.java:43)
at com.example.sofari.tiffanezni.Register$1.onClick(Register.java:74)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Register class:
  1. public class Register extends AppCompatActivity {  
  2.     DatabaseHelper db;  
  3.     Button btnchosen,btnajout;  
  4.    private  EditText editpseudo,editnom,editphone,editdob,editville,editsexe,editgroupe,editdatedon;  
  5.    private  ImageView pic;  
  6.     byte[] photo;  
  7.     private Contact datamodel;  
  8.    // private dataAdapter data;  
  9.     Bitmap bp;  
  10.     private String fpseudo,fnom,fphone,fdob,fville,fsexe,fgroupe,fdatedon;  
  11.   
  12.     @Override  
  13.     public void onCreate( Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_register);  
  16.        db = new DatabaseHelper(this);  
  17.   
  18.         editpseudo=(EditText)findViewById(R.id.input_pseudo);  
  19.         editnom=(EditText)findViewById(R.id.input_pseudo);  
  20.         editphone=(EditText)findViewById(R.id.input_pseudo);  
  21.         editdob=(EditText)findViewById(R.id.input_pseudo);  
  22.         editville=(EditText)findViewById(R.id.input_pseudo);  
  23.         editsexe=(EditText)findViewById(R.id.input_pseudo);  
  24.         editgroupe=(EditText)findViewById(R.id.input_pseudo);  
  25.         editdatedon=(EditText)findViewById(R.id.input_pseudo);  
  26.         pic=(ImageView)findViewById(R.id.imageView4);  
  27.         btnajout=(Button)findViewById(R.id.btn_enr);  
  28.         btnchosen=(Button)findViewById(R.id.btn_chosen);  
  29.          btnajout.setOnClickListener(new View.OnClickListener() {  
  30.              @Override  
  31.              public void onClick(View view) {  
  32.                  addContact();  
  33.              }  
  34.          });  
  35.           btnchosen.setOnClickListener(new View.OnClickListener() {  
  36.               @Override  
  37.               public void onClick(View view) {  
  38.                   selectImage();  
  39.               }  
  40.           });  
  41.              }  
  42.   
  43.   
  44.   
  45.     public void selectImage(){  
  46.         Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);  
  47.         photoPickerIntent.setType("image/*");  
  48.         startActivityForResult(photoPickerIntent, 2);  
  49.     }  
  50.   
  51.   
  52.     @Override  
  53.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  54.         switch(requestCode) {  
  55.             case 2:  
  56.                 if(resultCode == RESULT_OK){  
  57.                     Uri choosenImage = data.getData();  
  58.   
  59.                     if(choosenImage !=null){  
  60.   
  61.                         bp=decodeUri(choosenImage, 400);  
  62.                         pic.setImageBitmap(bp);  
  63.                     }  
  64.                 }  
  65.         }  
  66.     }  
  67.     protected Bitmap decodeUri(Uri selectedImage, int REQUIRED_SIZE) {  
  68.   
  69.         try {  
  70.   
  71.             // Decode image size  
  72.             BitmapFactory.Options o = new BitmapFactory.Options();  
  73.             o.inJustDecodeBounds = true;  
  74.             BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);  
  75.   
  76.             // The new size we want to scale to  
  77.             // final int REQUIRED_SIZE =  size;  
  78.   
  79.             // Find the correct scale value. It should be the power of 2.  
  80.             int width_tmp = o.outWidth, height_tmp = o.outHeight;  
  81.             int scale = 1;  
  82.             while (true) {  
  83.                 if (width_tmp / 2 < REQUIRED_SIZE  
  84.                         || height_tmp / 2 < REQUIRED_SIZE) {  
  85.                     break;  
  86.                 }  
  87.                 width_tmp /= 2;  
  88.                 height_tmp /= 2;  
  89.                 scale *= 2;  
  90.             }  
  91.             BitmapFactory.Options o2 = new BitmapFactory.Options();  
  92.             o2.inSampleSize = scale;  
  93.             return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);  
  94.         }  
  95.         catch (Exception e){  
  96.             e.printStackTrace();  
  97.         }  
  98.         return null;  
  99.     }  
  100.     @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)  
  101.     private byte[] profileImage(Bitmap b){  
  102.   
  103.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  104.         b.compress(Bitmap.CompressFormat.JPEG, 0, bos);  
  105.         return bos.toByteArray();  
  106.   
  107.     }  
  108.   
  109.     private void getValues(){  
  110.         fpseudo = editpseudo.getText().toString().trim();  
  111.                 fnom=editnom.getText().toString().trim();  
  112.                 fphone=editphone.getText().toString().trim();  
  113.                 fdob=editdob.getText().toString().trim();  
  114.                 fville=editville.getText().toString().trim();  
  115.                 fsexe=editsexe.getText().toString().trim();  
  116.                 fgroupe=editgroupe.getText().toString().trim();  
  117.                 fdatedon=editdatedon.getText().toString().trim();  
  118.                      photo = profileImage(bp);  
  119.     }  
  120.   
  121.   
  122.     //Insert data to the database  
  123.     private void addContact(){  
  124.         getValues();  
  125.   
  126.         db.addContacts(new Contact(fpseudo,fnom,fphone,fdob,fville,fsexe,fgroupe,fdatedon,photo));  
  127.         Toast.makeText(getApplicationContext(),"Saved successfully", Toast.LENGTH_LONG).show();  
  128.   
  129. }