getFloatArray is not receiving array - java

I'm working with a list:
List<Float> subjects = Arrays.asList(0f,0f,0f,0f,0f,0f);
List<Float> question9 = Arrays.asList(0f,0f);
And I want to put send it to another activity using Intent like this:
Intent iQuizz= new Intent(BachelorsResults.this, Results.class);
iQuizz.putExtra(BACHELOR, spinner.getSelectedItem().toString());
iQuizz.putExtra(QUESTION_9, question9.toArray());
iQuizz.putExtra(FAILED_SUBJECTS, subjects.toArray());
BachelorsResults.this.startActivity(iQuizz);
In the another activity I'm trying to receive float values:
Bundle extras = getIntent().getExtras();
float[] q9= extras.getFloatArray(QUESTION_9);
float[] fSubjects= extras.getFloatArray(FAILED_SUBJECTS);
But once I start this second activiy it launchs this Exception:
3-05 15:30:28.221 24640-24640/citrapp.exatec E/AndroidRuntime: FATAL EXCEPTION: main
Process: citrapp.exatec, PID: 24640
java.lang.RuntimeException: Unable to start activity ComponentInfo{citrapp.exatec/citrapp.exatec.Resultados}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at citrapp.exatec.Resultados.onCreate(Resultados.java:85)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Somehow when I pass them to the another activity, float arrays become nulls, because I test extras.getFloatArray inside the first activity and it shows the content, but in the second activity it doesn't. If somebody could help me would be awesome..
Thanks!

Try replacing getIntent().getExtras() with getIntent().getFloatArrayExtra().

Related

How do I "define" two other classes in a class so that I can use their methods

Currently I have already intialized
myDb = new DatabaseHelper(this);
and have no problem using DatabaseHelper's methods in my Main class. But now I'm trying to initialize another class
expenses = new AddExpenses();
and when I try to set the context for that it goes red and when I try to use AddExpenses class' method in my Main class, my app crashes.
The logcat when the app crashes :-
--------- beginning of crash
10-26 00:44:16.144 4105-4105/project.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: project.myapplication, PID: 4105
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor project.myapplication.DatabaseHelper.getLatestAmount(java.lang.String, java.lang.String)' on a null object reference
at project.myapplication.AddExpenses.recalculateAllSpendings(AddExpenses.java:273)
at project.myapplication.SpendingTracker$5.onClick(SpendingTracker.java:188)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference

I am new to openCV and android. I am trying to load an image from Resources using bitmap and process the image using open CV. It gave me this error:
java.lang.NullPointerException:
Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
Code:
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.a1);
((ImageView)findViewById(R.id.imageView)).setImageBitmap(bm);
Mat trainImage = new Mat(bm.getHeight(),bm.getWidth(),CvType.CV_32S);
Utils.bitmapToMat(bm,trainImage);
if(trainImage.empty()){
Log.e(TAG,"Empty image");
}
else{
Log.e(TAG,"Image Loaded");
}
Imgproc.cvtColor(trainImage, trainGray, Imgproc.COLOR_RGBA2GRAY);
Here is the Exception message:
E/SVM: Image Loaded
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pragyan.facefeatures, PID: 3308
java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:1724)
at com.example.pragyan.facefeatures.ExtractFeature.trainSvm(ExtractFeature.java:242)
at com.example.pragyan.facefeatures.ExtractFeature.onOptionsItemSelected(ExtractFeature.java:313)
at android.app.Activity.onMenuItemSelected(Activity.java:2885)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Can anyone tell me how to fix this. The image is loaded successfully and it still gives a NullPointerException.
I think you've not initialized the Mat trainGray. Try the following.
Mat trainGray = new Mat();
Imgproc.cvtColor(trainImage, trainGray, Imgproc.COLOR_RGBA2GRAY);

Trying to get Android_ID bu the App crashes when i start it

That's the Code to get the AndroidID:
String userID = Settings.Secure.getString(getApplicationContext()
.getContentResolver(), Settings.Secure.ANDROID_ID);
When i start the Application i get the following logcat review:
04-28 01:30:02.012 9328-9328/com.hubermoritz.beercounter.beercounter02 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hubermoritz.beercounter.beercounter02, PID: 9328
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hubermoritz.beercounter.beercounter02/com.hubermoritz.beercounter.beercounter02.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 
at android.app.ActivityThread.access$900(ActivityThread.java:174) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5593) 
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) 
04
Tried with:
public String userID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
also, didn't work either. By the way I'm well aware off that it's not a perfect way to recognize users, because the number can change due to factory reset or changing phone and few other ways.
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)
You are attempting to call a method that you are inheriting from Activity from a field initializer. Do not do this. In general, do not call inherited methods on Activity until after super.onCreate() has been called in your onCreate() method. You are welcome to have a field, but do not initialize it until after super.onCreate().
You can't use getContentResolver until after the constructor is completed. Set this variable in your onCreate instead and you should be ok

Not typical NullPointer exception

Ok, first of all, I know what is NullPointer but wanted to ask what could cause such a behaviour.
I received crash on Crittercism and stacktrace is pointing to List.add:
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.xxx.android.activities.SuperActivity.onCreate(SourceFile:59)
at com.xxx.android.activities.XXXActivity.onCreate(SourceFile:30)
But the fact is that there is no list in onCreate or even the whole SuperActivity.
What could cause such a stacktrace in Android? How can I find this NullPointer or similar source?
EDIT
As per request SuperActivity code around line 59:
super.onCreate(savedInstanceState);
if (activitiesCreated == 0) {
isFirstStart = true;
}
activitiesCreated++;
EDIT 2
I know how to follow stacktrace. What I want to know is what could be the problem and what can I do in cases like this?
EDIT 3
Stacktrace for the crashing thread:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.android/com.xxx.android.activities.XXXActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.xxx.android.activities.SuperActivity.onCreate(SourceFile:59)
at com.xxx.android.activities.XXXActivity.onCreate(SourceFile:30)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
... 9 more
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.xxx.android.activities.SuperActivity.onCreate(SourceFile:59)
at com.xxx.android.activities.XXXActivity.onCreate(SourceFile:30)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Why do i get a NullPointerException when I have checked for the the String not being null?

My app crashes on a NPE even though i'm checking whether the value of the String is non null:
if (AppConstants.AppConstants_appName != null &&
AppConstants.AppConstants_appName.equals("xxx")) {
suffix = "version=1.0";
}
AppConstants_appName is a public static String.
Not sure what's going wrong here.
Here's my Stacktrace. Anyone an idea?
Stacktrace
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at nl.onlineretailservice.library.APIManager.getURLSuffix(APIManager.java:637)
at nl.onlineretailservice.library.APIManager.requestFolderProducts(APIManager.java:334)
at nl.onlineretailservice.helper.FolderViewerActivityHelper.loadProducts(FolderViewerActivityHelper.java:160)
at nl.onlineretailservice.helper.FolderViewerActivityHelper$2.downloadReady(FolderViewerActivityHelper.java:267)
at nl.onlineretailservice.library.DownloadOperation.onPostExecute(DownloadOperation.java:221)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Categories