0

if anyone could help with this that would be great!

I keep getting a null pointer error which is odd because the application was working fine a few minutes ago and I cannot see what is wrong, it crashes on launch. If you need anything else that will help then let me know, here's all the details:

Logcat:

04-08 23:31:27.579    5906-5906/com.yupo.dominic.yupo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yupo.dominic.yupo/com.yupo.dominic.yupo.LectureLogged}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.yupo.dominic.yupo.MenuFragment1.onCreateView(MenuFragment1.java:49)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
        at android.app.Activity.performStart(Activity.java:5143)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

Manifest file (If it's helpful):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yupo.dominic.yupo" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".UserAuth"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Login"
        android:label="@string/title_login_area">
    </activity>
    <activity
        android:name=".StudentLogged"
        android:label="@string/title_student_area">
    </activity>
    <activity
        android:name=".Register"
        android:label="@string/register">
    </activity>
    <activity
        android:name=".LectureLogged"
        android:label="@string/title_lecture_area">
    </activity>
    <activity android:name=".MessagingActivity"
        android:screenOrientation="portrait">
    </activity>
    <service android:name=".MessageService">

    </service>
</application>

MenuFragment1 Class:

package com.yupo.dominic.yupo;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseUser;

/**
 * Created by Dominic on 03/03/2015.
 */
public class MenuFragment1 extends Fragment implements View.OnClickListener{

    View rootview;
    Spinner uniSpinner;
    Button changeUniButton;
    String uniSpinnerText;
    ParseUser currentUser;
    ParseQueryAdapter<ParseObject> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        currentUser = ParseUser.getCurrentUser();

        if(currentUser.getString("type").equalsIgnoreCase("Lecturer"))
        {
            rootview = inflater.inflate(R.layout.menu1_lecture, container, false);

            TextView lectureUniTextView = (TextView) rootview.findViewById(R.id.lectureUniTextView);
            TextView lectureChangeUniTextView = (TextView) rootview.findViewById(R.id.lectureChangeUniTextView);
            uniSpinner = (Spinner) rootview.findViewById(R.id.uniSpinner);
            uniSpinnerSetup();
            changeUniButton = (Button) rootview.findViewById(R.id.changeUniButton);
            changeUniButton.setOnClickListener(this);

            if(currentUser.getString("university").equals(""))
            {
                lectureUniTextView.setText("You do not have a University set!");
                lectureChangeUniTextView.setText("Select a University");
                changeUniButton.setText("Set");

            }
            else
            {
                lectureUniTextView.setText("Your current University is: " + currentUser.getString("university"));
                lectureChangeUniTextView.setText("Change University to");
                changeUniButton.setText("Change");
            }
        }
        else
        {
            rootview = inflater.inflate(R.layout.menu1_student, container, false);

            TextView studentUniTextView = (TextView) rootview.findViewById(R.id.studentUniTextView);
            TextView studentChangeUniTextView = (TextView) rootview.findViewById(R.id.studentChangeUniTextView);
            uniSpinner = (Spinner) rootview.findViewById(R.id.uniSpinner);
            uniSpinnerSetup();
            changeUniButton = (Button) rootview.findViewById(R.id.changeUniButton);
            changeUniButton.setOnClickListener(this);

            if(currentUser.getString("university").equals(""))
            {
                studentUniTextView.setText("You do not have a University set!");
                studentChangeUniTextView.setText("Select a University");
                changeUniButton.setText("Set");

            }
            else
            {
                studentUniTextView.setText("Your current University is: " + currentUser.getString("university"));
                studentChangeUniTextView.setText("Change University to");
                changeUniButton.setText("Change");
            }
        }

        return rootview;
    }

    public void uniSpinnerSetup()
    {
        ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
            public ParseQuery create() {
                ParseQuery query = new ParseQuery("University");
                return query;
            }
        };

        adapter = new ParseQueryAdapter<ParseObject>(getActivity(), factory);
        adapter.setTextKey("name");
        uniSpinner.setAdapter(adapter);
        uniSpinner.setSelection(1);
        uniSpinner.setOnItemSelectedListener(new mySpinnerListener());
    }

    class mySpinnerListener implements Spinner.OnItemSelectedListener
    {
        @Override
        public void onItemSelected(AdapterView parent, View v, int position,long id) {

            ParseObject theSelectedObject = adapter.getItem(position);
            uniSpinnerText = theSelectedObject.get("name").toString();
        }

        @Override
        public void onNothingSelected(AdapterView parent) {
            // TODO Auto-generated method stub
            // Do nothing.
        }

    }

    @Override
    public void onClick(View v) {

        if(currentUser.getString("university").equals(uniSpinnerText))
        {
            Toast.makeText(getActivity(),"Your University is already set to that!", Toast.LENGTH_LONG).show();
        }
        else
        {
            currentUser.put("university", uniSpinnerText);
            currentUser.saveInBackground();
            Toast.makeText(getActivity(), "University changed to: " + uniSpinnerText, Toast.LENGTH_LONG).show();
        }
    }
}

menu1_student.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/selectUniFrame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Your Selected University: "
            android:id="@+id/studentUniTextView"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/changeUni"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/selectUniFrame">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Change University to"
            android:id="@+id/studentChangeUniTextView"
            android:layout_alignParentLeft="true"/>

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/uniSpinner"
            android:visibility="visible"
            android:layout_below="@+id/studentChangeUniTextView"
            android:spinnerMode="dropdown"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/changeUniButton"
            android:layout_toRightOf="@+id/uniSpinner"
            android:text="Change"/>

    </RelativeLayout>

</RelativeLayout>

menu1_lecture.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/selectUniFrame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Your Selected University: "
            android:id="@+id/lectureUniTextView"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/changeUni"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/selectUniFrame">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Change University to"
            android:id="@+id/lectureChangeUniTextView"
            android:layout_alignParentLeft="true"/>

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/uniSpinner"
            android:visibility="visible"
            android:layout_below="@+id/lectureChangeUniTextView"
            android:spinnerMode="dropdown"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/changeUniButton"
            android:layout_toRightOf="@+id/uniSpinner"
            android:text="Change"/>

    </RelativeLayout>

</RelativeLayout>
vmetelz
  • 87
  • 10

2 Answers2

0

Instead of

if(currentUser.getString("university").equals(""))

Use

if(currentUser==null || currentUser.getString("university")==null || currentUser.getString("university").equals(""))
Kartik
  • 7,677
  • 4
  • 28
  • 50
0

It looks like there is no university key in your currentUser object.

It was probably populated at one point, and that's why it was working before.

Either it's not populated if you uninstall/re-install, or you have some code somewhere that cleared the value.

To answer your question about expected (Undefined) behavior, this is from the Parse documentation:

getString

public String getString(String key)

Access a String value.

Parameters: key - The key to access the value for.

Returns: null if there is no such key or if it is not a String

So, returning null in this case is expected behavior.

Reference: https://parse.com/docs/android/api/com/parse/ParseObject.html#getString(java.lang.String)

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137