TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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:
public
class
Register
extends
AppCompatActivity {
DatabaseHelper db;
Button btnchosen,btnajout;
private
EditText editpseudo,editnom,editphone,editdob,editville,editsexe,editgroupe,editdatedon;
private
ImageView pic;
byte
[] photo;
private
Contact datamodel;
// private dataAdapter data;
Bitmap bp;
private
String fpseudo,fnom,fphone,fdob,fville,fsexe,fgroupe,fdatedon;
@Override
public
void
onCreate( Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
db =
new
DatabaseHelper(
this
);
editpseudo=(EditText)findViewById(R.id.input_pseudo);
editnom=(EditText)findViewById(R.id.input_pseudo);
editphone=(EditText)findViewById(R.id.input_pseudo);
editdob=(EditText)findViewById(R.id.input_pseudo);
editville=(EditText)findViewById(R.id.input_pseudo);
editsexe=(EditText)findViewById(R.id.input_pseudo);
editgroupe=(EditText)findViewById(R.id.input_pseudo);
editdatedon=(EditText)findViewById(R.id.input_pseudo);
pic=(ImageView)findViewById(R.id.imageView4);
btnajout=(Button)findViewById(R.id.btn_enr);
btnchosen=(Button)findViewById(R.id.btn_chosen);
btnajout.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
addContact();
}
});
btnchosen.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
selectImage();
}
});
}
public
void
selectImage(){
Intent photoPickerIntent =
new
Intent(Intent.ACTION_PICK);
photoPickerIntent.setType(
"image/*"
);
startActivityForResult(photoPickerIntent,
2
);
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
switch
(requestCode) {
case
2
:
if
(resultCode == RESULT_OK){
Uri choosenImage = data.getData();
if
(choosenImage !=
null
){
bp=decodeUri(choosenImage,
400
);
pic.setImageBitmap(bp);
}
}
}
}
protected
Bitmap decodeUri(Uri selectedImage,
int
REQUIRED_SIZE) {
try
{
// Decode image size
BitmapFactory.Options o =
new
BitmapFactory.Options();
o.inJustDecodeBounds =
true
;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage),
null
, o);
// The new size we want to scale to
// final int REQUIRED_SIZE = size;
// Find the correct scale value. It should be the power of 2.
int
width_tmp = o.outWidth, height_tmp = o.outHeight;
int
scale =
1
;
while
(
true
) {
if
(width_tmp /
2
< REQUIRED_SIZE
|| height_tmp /
2
< REQUIRED_SIZE) {
break
;
}
width_tmp /=
2
;
height_tmp /=
2
;
scale *=
2
;
}
BitmapFactory.Options o2 =
new
BitmapFactory.Options();
o2.inSampleSize = scale;
return
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage),
null
, o2);
}
catch
(Exception e){
e.printStackTrace();
}
return
null
;
}
@TargetApi
(Build.VERSION_CODES.HONEYCOMB_MR1)
private
byte
[] profileImage(Bitmap b){
ByteArrayOutputStream bos =
new
ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG,
0
, bos);
return
bos.toByteArray();
}
private
void
getValues(){
fpseudo = editpseudo.getText().toString().trim();
fnom=editnom.getText().toString().trim();
fphone=editphone.getText().toString().trim();
fdob=editdob.getText().toString().trim();
fville=editville.getText().toString().trim();
fsexe=editsexe.getText().toString().trim();
fgroupe=editgroupe.getText().toString().trim();
fdatedon=editdatedon.getText().toString().trim();
photo = profileImage(bp);
}
//Insert data to the database
private
void
addContact(){
getValues();
db.addContacts(
new
Contact(fpseudo,fnom,fphone,fdob,fville,fsexe,fgroupe,fdatedon,photo));
Toast.makeText(getApplicationContext(),
"Saved successfully"
, Toast.LENGTH_LONG).show();
}
Reply
Answers (
0
)
I Want To Convert DATE in dd/mm/yyyy in Android
App crash when I log in