eddie2103
Lost but trying
Profile
Posts: 1
Reg: Nov 19, 2014
Sabak Bernam
10
11/19/14 11:19 PM (9 years ago)

E/AndroidRuntime(530): FATAL EXCEPTION: main (Unfortunately app is stop)

good day, i have a problem when i run my code in emulator the error come out and i cannot find where the problem is, here the error log 11-20 05:27:15.540: E/AndroidRuntime(530): FATAL EXCEPTION: main 11-20 05:27:15.540: E/AndroidRuntime(530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shishkin.steganographie/com.shishkin.steganographie.ui.MainActivity}: java.lang.NullPointerException 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.ActivityThread.access$600(ActivityThread.java:123) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.os.Handler.dispatchMessage(Handler.java:99) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.os.Looper.loop(Looper.java:137) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.ActivityThread.main(ActivityThread.java:4424) 11-20 05:27:15.540: E/AndroidRuntime(530): at java.lang.reflect.Method.invokeNative(Native Method) 11-20 05:27:15.540: E/AndroidRuntime(530): at java.lang.reflect.Method.invoke(Method.java:511) 11-20 05:27:15.540: E/AndroidRuntime(530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 11-20 05:27:15.540: E/AndroidRuntime(530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 11-20 05:27:15.540: E/AndroidRuntime(530): at dalvik.system.NativeStart.main(Native Method) 11-20 05:27:15.540: E/AndroidRuntime(530): Caused by: java.lang.NullPointerException 11-20 05:27:15.540: E/AndroidRuntime(530): at com.shishkin.steganographie.ui.MainActivity.onCreate(MainActivity.java:88) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.Activity.performCreate(Activity.java:4465) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 11-20 05:27:15.540: E/AndroidRuntime(530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 11-20 05:27:15.540: E/AndroidRuntime(530): ... 11 more THE CODE public class MainActivity extends SherlockActivity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback { private static final String LOG_TAG = MainActivity.class.getSimpleName(); private static final int SELECT_IMAGE = 0; private static final int MESSAGE_SENT = 1; private ImageView imageView; private TextView textView; private File image; private NfcAdapter mNfcAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Log.v(LOG_TAG, "onCreate() called"); super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(android.R.id.icon); textView = (TextView) findViewById(android.R.id.text1); // Check for available NFC Adapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter == null) { Toast.makeText(this, "NFC is not available on this device", Toast.LENGTH_LONG).show(); } // Register callback to set NDEF message mNfcAdapter.setNdefPushMessageCallback(this, this); // Register callback to listen for message-sent success mNfcAdapter.setOnNdefPushCompleteCallback(this, this); } /** * Implementation for the CreateNdefMessageCallback interface */ @Override public NdefMessage createNdefMessage(NfcEvent event) { Log.v(LOG_TAG, "createNdefMessage() called"); NdefMessage msg = null; try { msg = new NdefMessage( new NdefRecord[] { createMimeRecord( "application/com.shishkin.steganographie", FileUtils.readFileToByteArray(image)) /** * The Android Application Record (AAR) is commented out. When a device * receives a push with an AAR in it, the application specified in the AAR * is guaranteed to run. The AAR overrides the tag dispatch system. * You can add it back in to guarantee that this * activity starts when receiving a beamed message. For now, this code * uses the tag dispatch system. */ // ,NdefRecord.createApplicationRecord("com.shishkin.steganographie") }); } catch (IOException e) { e.printStackTrace(); } return msg; } /** * Implementation for the OnNdefPushCompleteCallback interface */ @Override public void onNdefPushComplete(NfcEvent arg0) { Log.v(LOG_TAG, "onNdefPushComplete() called"); // A handler is needed to send messages to the activity when this // callback occurs, because it happens from a binder thread mHandler.obtainMessage(MESSAGE_SENT).sendToTarget(); } /** This handler receives a message from onNdefPushComplete */ private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_SENT: Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show(); break; } } }; @Override public void onResume() { Log.v(LOG_TAG, "onResume() called"); super.onResume(); // Check to see that the Activity started due to an Android Beam if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { processIntent(getIntent()); } } @Override public void onNewIntent(Intent intent) { Log.v(LOG_TAG, "onNewIntent() called"); // onResume gets called after this to handle the intent setIntent(intent); } /** * Parses the NDEF Message from the intent and prints to the TextView */ void processIntent(Intent intent) { Log.v(LOG_TAG, "processIntent() called"); Parcelable[] rawMsgs = intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES); // only one message sent during the beam NdefMessage msg = (NdefMessage) rawMsgs[0]; // record 0 contains the MIME type, record 1 is the AAR, if present byte[] bytes = msg.getRecords()[0].getPayload(); //write the bytes in file try { image = new File(Environment.getExternalStorageDirectory() + "/Steganography/" + "foto.gif"); if (!image.getParentFile().exists()) { image.getParentFile().mkdirs(); } FileOutputStream fos = new FileOutputStream(image); try { fos.write(bytes); } finally { fos.close(); } imageView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length)); } catch (IOException e) { e.printStackTrace(); } } /** * Creates a custom MIME type encapsulated in an NDEF record * * @param mimeType */ public NdefRecord createMimeRecord(String mimeType, byte[] payload) { Log.v(LOG_TAG, "createMimeRecord() called"); byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII")); NdefRecord mimeRecord = new NdefRecord( NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload); return mimeRecord; } @Override public boolean onCreateOptionsMenu(Menu menu) { Log.v(LOG_TAG, "onCreateOptionsMenu() called"); menu.add(Menu.NONE, R.id.menu_select_image, Menu.NONE, R.string.menu_select_image) .setIcon(R.drawable.ic_action_gallery) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.add(Menu.NONE, R.id.menu_encrypt, Menu.NONE, R.string.menu_encrypt) .setIcon(R.drawable.ic_action_encrypt) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.add(Menu.NONE, R.id.menu_decrypt, Menu.NONE, R.string.menu_decrypt) .setIcon(R.drawable.ic_action_decrypt) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.add(Menu.NONE, R.id.menu_preferences, Menu.NONE, R.string.menu_preferences) .setIcon(R.drawable.ic_action_decrypt) .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { Log.v(LOG_TAG, "onOptionsItemSelected() called"); switch (item.getItemId()) { case R.id.menu_select_image: selectImage(); break; case R.id.menu_encrypt: encryptImage(); break; case R.id.menu_decrypt: decryptImage(); break; case R.id.menu_preferences: invokePreferencesActivity(); break; default: return super.onOptionsItemSelected(item); } return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.v(LOG_TAG, "onActivityResult() called"); super.onActivityResult(requestCode, resultCode, data); if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK) { Uri selectedImageUri = data.getData(); String selectedImagePath = null; try { //MEDIA GALLERY selectedImagePath = getPath(selectedImageUri); } catch (Exception e) { //OI FILE Manager selectedImagePath = selectedImageUri.getPath(); } Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath); imageView.setImageBitmap(bitmap); image = new File(selectedImagePath); updatePossibleTextLength(); } } /** * Select Image from SD card * (http://stackoverflow.com/questions/2507898/how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android) */ private void selectImage() { Log.v(LOG_TAG, "selectImage() called"); Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, SELECT_IMAGE); } /** * Convert URI to image path. * * @param uri * @return image path */ private String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; @SuppressWarnings("deprecation") Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } /** * Update Possible Text Length. Called when changing graphics container. */ @SuppressWarnings("static-access") private void updatePossibleTextLength() { Log.v(LOG_TAG, "updatePossibleTextLength() called"); EncryptingFileParameters params = null; SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); try { if (preferences.getString("encryptionMethodPref", "lsb").equals("lsb")) { params = new GIFEncryptorByLSBMethod().getEncryptingFileParameters(image); } else { params = new GIFEncryptorByPaletteExtensionMethod().getEncryptingFileParameters(image); } textView.setText(String.format("Possible message length: %d", params.getPossibleTextLength())); textView.setVisibility(View.VISIBLE); } catch (Exception e) { Log.w(LOG_TAG, e); textView.setVisibility(View.INVISIBLE); } } /** * Start Preference activity to display the settings. */ private void invokePreferencesActivity() { Log.v(LOG_TAG, "invokePreferencesActivity() called"); startActivity(new Intent(this, PreferencesActivity.class)); } /** * Formation of steganographic messages using selected encryption method. */ private void encryptImage() { Log.v(LOG_TAG, "encryptImage() called"); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); IEncryptor encryptor = preferences.getString("encryptionMethodPref", "lsb").equals("lsb") ? new GIFEncryptorByLSBMethod() : new GIFEncryptorByPaletteExtensionMethod(); ICompressor compressor = new StringCompressor(); ICrypto crypto = new AESCrypto(); // obtaining the original message String message = preferences.getString("messagePref", ""); // obtaining the encryption key String key = preferences.getString("keyPref", ""); try { // encrypt the original message byte[] encryptedData = crypto.encrypt(compressor.compress(message.getBytes()), key); File out = new File(Environment.getExternalStorageDirectory() + "/Steganography/" + image.getName()); if (!out.getParentFile().exists()) { out.getParentFile().mkdirs(); } // introduction of an encrypted message to the graphics container encryptor.encrypt(image, out, encryptedData); imageView.setImageBitmap(BitmapFactory.decodeFile(out.getPath())); image = out; Toast.makeText(this, Parameters.MESSAGE_ENCRYPTION_COMPLETED, Toast.LENGTH_LONG).show(); } catch (UnableToEncodeException e) { Toast.makeText(this, Parameters.MESSAGE_ENCRYPTION_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } catch (IOException e) { Toast.makeText(this, Parameters.MESSAGE_IO_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } catch (NullPointerException e) { Toast.makeText(this, Parameters.MESSAGE_UNEXPECTED_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } catch (GeneralSecurityException e) { Toast.makeText(this, Parameters.MESSAGE_ENCRYPTION_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } } /** * Decrypt steganographic messages using selected encryption * method and display the encrypted message. */ private void decryptImage() { Log.v(LOG_TAG, "decryptImage() called"); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); IEncryptor encryptor = preferences.getString("encryptionMethodPref", "lsb").equals("lsb") ? new GIFEncryptorByLSBMethod() : new GIFEncryptorByPaletteExtensionMethod(); ICompressor compressor = new StringCompressor(); ICrypto crypto = new AESCrypto(); // obtaining the encryption key String key = preferences.getString("keyPref", ""); try { // extracting hidden data from the image byte[] data = encryptor.decrypt(image); // decoding of hidden data String decryptedMsg = new String(compressor.decompress(crypto.decrypt(data, key))); Toast.makeText(this, "decrypted message: " + decryptedMsg, Toast.LENGTH_LONG).show(); } catch (UnableToDecodeException e) { Toast.makeText(this, Parameters.MESSAGE_DECRYPTION_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } catch (IOException e) { Toast.makeText(this, Parameters.MESSAGE_IO_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } catch (NullPointerException e) { Toast.makeText(this, Parameters.MESSAGE_UNEXPECTED_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } catch (GeneralSecurityException e) { Toast.makeText(this, Parameters.MESSAGE_DECRYPTION_ERROR, Toast.LENGTH_LONG).show(); Log.w(LOG_TAG, e); } } }
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
11/20/14 12:01 AM (9 years ago)
java.lang.NullPointerException usually means that something is not initialized, like not initialized at all, and you are still trying to use it in the program. One way to see what it is is to surround critical parts of the program with try catch clause: select a block of code, then with Alt Shift Z in Eclipse under Windows, try to get the clause semi-automatically. The error will not stop the program after that, but you will have to take a close look at LogCat listings to see what happens. The other way is prosaic: put BT_debugger code before and after offending statements and pinpoint from there. The third way is to use the debug function and perspective in Eclipse. With Ctrl Shift B mark breakpoint in the code and in the debug perspective, use windows called Expressions to monitor the values in the program. So...
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
11/20/14 12:05 AM (9 years ago)
To be just a more concrete, there is a line com.shishkin.steganographie.ui.MainActivity.onCreate(MainActivity.java:88) So, have a look at the line number 88 in MainActivity; the error comes right after that. The best way to find is a double click on that line, and after a few seconds (be patient here), Eclipse will show you the offending line in the editor window. In general, when studying the LogCat output, pay attention only to the lines which names of the activities: 99% of time, the error took place after that.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
11/20/14 12:41 AM (9 years ago)
I echo Dusko. Except the part where I say 'Welcome to Buzztouch'. And the other part where I say 'we usually just support Buzztouch projects' ;) Cheers! -- Smug
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.