-1

I am a complete newbie in android development. At the moment I am trying to replicate an app I saw on where it is possible to convert multiple images (jpeg, png, jpg) into a single pdf file. I have tried to do the most I can with debugging. But I don't understand why there are still this NullPointer Exception.

Oh and by the way please pardon me if I have some mistakes in placing proper tags or such in my question this is my first question thank you.

Here's my code for the ConvertToPDFConverter.java

package com.example.lenovo_thinkpad.mobilelectureboard;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;


public class ConvertToPDFConverter extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawer;
android.support.v7.app.ActionBar bar;
CustomActionBarDrawerToggle toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_convert_to_pdfconverter);

    bar = this.getSupportActionBar();

    assert bar != null;
    bar.setDisplayHomeAsUpEnabled(true);

    bar.setHomeButtonEnabled(true);
    bar.setDisplayShowTitleEnabled(false);

    drawer =  findViewById(R.id.drawer_layout);
    drawer.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

    try {
        toggle = new CustomActionBarDrawerToggle(this, drawer);
    } catch (RuntimeException e) {
        e.printStackTrace();
    }

    drawer.addDrawerListener(toggle);

    // Set navigation drawer

    // Set com.example.lenovo_thinkpad.mobilelectureboard.Home fragment
    Fragment fragment = new Home();
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment fragment;
    FragmentManager fragmentManager = getSupportFragmentManager();

    switch (id) {
        case R.id.nav_camera:
            fragment = new Home();
            fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
            break;
        case R.id.nav_gallery:
            fragment = new ViewFiles();
            fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
            break;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

    CustomActionBarDrawerToggle(Activity mActivity,
                                DrawerLayout mDrawerLayout) {
        super(mActivity, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    }

    @Override
    public void onDrawerClosed(View view) {
        bar.setTitle(getString(R.string.navigation_drawer_close));
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }

    @Override
    public void onDrawerOpened(View drawerView) {
        bar.setTitle(getString(R.string.navigation_drawer_open));
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }
}

}

and for the layout activity_convert_to_pdfconverter.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ConvertToPDFConverter">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="@color/mb_white"
        android:id="@+id/content"/>

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

Here's the stacktrace

02-13 15:12:17.976 1548-1548/com.example.lenovo_thinkpad.mobilelectureboard E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                              Process: com.example.lenovo_thinkpad.mobilelectureboard, PID: 1548
                                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo_thinkpad.mobilelectureboard/com.example.lenovo_thinkpad.mobilelectureboard.ConvertToPDFConverter}: java.lang.NullPointerException
                                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                                  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:785)
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                                                               Caused by: java.lang.NullPointerException
                                                                                                  at com.example.lenovo_thinkpad.mobilelectureboard.ConvertToPDFConverter.onCreate(ConvertToPDFConverter.java:34)
                                                                                                  at android.app.Activity.performCreate(Activity.java:5231)
                                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                                  at android.os.Looper.loop(Looper.java:136) 
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                                                  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:785) 
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                                                  at dalvik.system.NativeStart.main(Native Method) 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme_picker">
    <activity
        android:name=".MainActivity"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".WhiteBoardActivity" />
    <activity android:name=".ConvertToPDFConverter" />
</application>

  • Please add the Logcat as text not as picture. And there seem to be some (the most interesting) lines missing. Last not least, which line is line 34? – Bö macht Blau Feb 13 '18 at 20:04
  • should I add the logcat as code also? sorry I'm new here. and line 34 is the "bar.setDisplayHomeAsUpEnabled(false);" line – jeffbu-Dev Feb 13 '18 at 20:05
  • I also did not add the imports as I thought those were unnecessary but everything else is in there. I also have separate layouts for other fragments if you wish to see those I would provide it. – jeffbu-Dev Feb 13 '18 at 20:08
  • Yes, we like to have the Logcat as code instead of as picture. And since you post code without line numbers it's always helpful to know in which line of the code we are seeing the crash occured. I'm going to flag your question as a duplicate of an existing, quite generic question because I think if you understand the answers to the other question then you will be able to avoid the NullPointerException. – Bö macht Blau Feb 13 '18 at 20:12
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bö macht Blau Feb 13 '18 at 20:12
  • I've read about that Post and tried to do what I can accordingly. I created a method to invoke the creation of the drawer, I thought that would create a non-null object but I still get the NullPointerException. I'm really sorry I'm so bad at actual coding. I have also added the stack trace. – jeffbu-Dev Feb 13 '18 at 20:17
  • Don't be sorry, we all started from zero some day. Did the answer below help? (I thought it might) – Bö macht Blau Feb 13 '18 at 20:19
  • One more thing: you call `setContentView(R.layout.activity_main);` but you say your layout file is named *activity_convert_to_pdfconverter.xml*. – Bö macht Blau Feb 13 '18 at 20:22
  • I'm still on it at the moment. – jeffbu-Dev Feb 13 '18 at 20:22
  • hmmm. i didn't noticed that. Actually this app I'm building right now is like a compilation of a few apps so I didn't notice a few things when I was transferring the codes. thanks for pointing that out. – jeffbu-Dev Feb 13 '18 at 20:24

1 Answers1

1

Since you are extending the AppCompatActivity, try to get the actionbar via 'getSupportActionbar()'.

G. Kalender
  • 491
  • 4
  • 13
  • I'll try it now. one moment. – jeffbu-Dev Feb 13 '18 at 20:09
  • getSupportActionBar() seems to return null. Can you show your androidmanifest file? Maybe a duplicate of https://stackoverflow.com/questions/41741165/getsupportactionbar-returns-null-in-android-app ? – G. Kalender Feb 13 '18 at 20:35
  • Oh, you are using "setContentView(R.layout.activity_main);", it should be "setContentView(R.layout. activity_convert_to_pdfconverter);" – G. Kalender Feb 13 '18 at 20:39
  • I have added the manifest. I noticed that also and immediately changed it. I'll edit the updated code – jeffbu-Dev Feb 13 '18 at 20:44
  • Your manifest shows, that you're using "android:theme="@style/AppTheme_picker" as a theme. You should check, what theme the AppTheme_picker is extending. – G. Kalender Feb 13 '18 at 20:46
  • should I use the parent theme for that? it extends Theme.AppCompat.Light.DarkActionBar – jeffbu-Dev Feb 13 '18 at 20:48
  • Okay, so it has an ActionBar-theme. Is the stacktrace still the nullpointer in line 34? – G. Kalender Feb 13 '18 at 20:52
  • When I added the assert bar != null line the stack trace moved to line 36. – jeffbu-Dev Feb 13 '18 at 20:54
  • 1
    You've got a toolbar in the layout. In this case you should use a theme like '.NoActionBar' and call 'setSupportActionBar(findViewById(R.id.toolbar))' before you call 'getSupportActionbar()'. For further info read this https://developer.android.com/training/appbar/setting-up.html – G. Kalender Feb 13 '18 at 21:04
  • I created a theme under that DarkActionbar without an auto generated ToolBar. The AppTheme_picker – jeffbu-Dev Feb 13 '18 at 21:07
  • But wait let me try that. – jeffbu-Dev Feb 13 '18 at 21:08
  • Omg. This finally fixed it. I understand what I was doing wrong. Thanks man. You're a beast. – jeffbu-Dev Feb 13 '18 at 22:27