Dropbox API 22 error: "Attempt to invoke virtual method"

1

I have been trying for days to make an Android application that uploads and downloads a Dropbox file using its API and there is no way, it gives me the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.dropbox.client2.session.Session com.dropbox.client2.DropboxAPI.getSession()' on a null object reference

I leave the code to see if you see where the error comes from.

package com.example.andrs.petw_final;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AppKeyPair;

import java.io.File;
import java.io.FileInputStream;

public class MainActivity extends AppCompatActivity {

final static private String APP_KEY = "s1snkdm8xjqwp5";
final static private String APP_SECRET ="uuap3et6grvrkv";

// In the class declaration section:
private DropboxAPI<AndroidAuthSession> mDBApi;



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

    // callback method
    initialize_session();


}




protected void initialize_session(){

    // In the class declaration section:
    DropboxAPI<AndroidAuthSession> mDBApi;

    // And later in some initialization function:
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);

    mDBApi.getSession().startOAuth2Authentication(MainActivity.this);


}

public void uploadFiles(View view){

    new Upload().execute();
}

public class  Upload extends AsyncTask<String,Void,String>{

    protected void onPreExecute(){

    }

    protected String doInBackground(String... arg0){

        DropboxAPI.Entry response = null;

        try {

            // Define path of file to be upload
            File file = new File("./sdcard/images.jpg");
            FileInputStream inputStream = new FileInputStream(file);

            //put the file to dropbox
            response = mDBApi.putFile("/screens.png", inputStream,
                    file.length(), null, null);
            Log.e("DbExampleLog", "The uploaded file's rev is: " + response.rev);

        } catch (Exception e){

            e.printStackTrace();
        }

        return response.rev;
    }

    @Override
    protected void onPostExecute(String result) {
        if(result.isEmpty() == false){
            Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
            Log.e("DbExampleLog", "The uploaded file's rev is: " + result);
        }
    }
}



protected void onResume() {

    super.onResume();

    if (mDBApi.getSession().authenticationSuccessful()) {
        try {
            // Required to complete auth, sets the access token on the session
            mDBApi.getSession().finishAuthentication();

            String accessToken = mDBApi.getSession().getOAuth2AccessToken();
        } catch (IllegalStateException e) {
            Log.i("DbAuthLog", "Error authenticating", e);
        }
    }
}
}

Thank you.

Now I get this other exception:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.andrs.petw_final/com.example.andrs.petw_final.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.andrs.petw_final.MainActivity> has no zero argument constructor

Thanks Jorgesys for your answer

    
asked by Gigasnike95 08.08.2017 в 13:34
source

1 answer

0

The problem you define in your question:

  

java.lang.NullPointerException: Attempt to invoke virtual method   'com.dropbox.client2.session.Session   com.dropbox.client2.DropboxAPI.getSession () 'on a null object   reference

is caused by calling the mDBApi.getSession() method in a DropboxAPI instance that has a null value.

But the real problem is that the session is not being created successfully, it ensures that the credentials are correct. I recommend validating in case of any problem when creating the session:

protected void initialize_session(){

    // In the class declaration section:
    DropboxAPI<AndroidAuthSession> mDBApi;

    // And later in some initialization function:
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);

   if (session.authenticationSuccessful()) { // * Valida si autenticación es correcta!
     mDBApi = new DropboxAPI<AndroidAuthSession>(session);
     mDBApi.getSession().startOAuth2Authentication(MainActivity.this);
   }else{
      //Error al autenticar, revisar credenciales!
      Log.i("DbAuthLog", "Error al autenticar, revisar credenciales.");
   }

}

It is important to know that the Activity that performs the Authentication must have defined the db-APP_KEY scheme, this within the AndroidManifest.xml file.

    <activity 
        ...
        ...
        <intent-filter>
            <!-- Cambiar a db- seguido de tu app key -->
            <data android:scheme="db-xxxxxxxxxxxx"/>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    
answered by 08.08.2017 / 17:29
source