0

I've been stucked to find what's wrong with my code below.

here's the MainActivity.java

package com.andri.hilltest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    EditText txtPlain, txtKey, txtCipher;
    Button btnEncrypt;
    String char_db = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc";
    int array_angka[];
    int i;

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


        txtPlain = (EditText) findViewById(R.id.txtPlain);
        txtKey = (EditText) findViewById(R.id.txtKey);
        txtCipher = (EditText) findViewById(R.id.txtCipher);

        btnEncrypt = (Button) findViewById(R.id.btnEncrypt);

        btnEncrypt.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if(v == btnEncrypt){

            String plainTextInput = txtPlain.getText().toString();

            char[] array_huruf = plainTextInput.toCharArray();
            //int[] angka2;


            for (i=0 ; i < array_huruf.length ; i++){

                int posisi_huruf =  char_db.indexOf(array_huruf[i]);

                Toast.makeText(this, "Tombol ditekan " +  posisi_huruf , Toast.LENGTH_SHORT).show();
                array_angka[i] = posisi_huruf; //if I disable this line the code should run well
            }

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}

Here's the MainActivity.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Plaintext"
        />
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtPlain"
        android:hint="Masukkan plaintext"
        />
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Key"
        />
    <EditText 
        android:layout_width="fill_parent"
        android:hint="Masukkan Key"
        android:layout_height="wrap_content"
        android:id="@+id/txtKey"
        />
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Encrypt"
        android:id="@+id/btnEncrypt"
        />
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cipher (Hill Cipher)"
        />
    <EditText 
        android:layout_width="fill_parent"
        android:hint=""
        android:layout_height="wrap_content"
        android:id="@+id/txtCipher"
        />
</LinearLayout>

the problem is when I put a line of code to insert the value of an int array from a loop. the when I ran and put some string as input, I got this error message

Log.txt

04-11 10:50:22.039: D/AndroidRuntime(17743): Shutting down VM
04-11 10:50:22.039: W/dalvikvm(17743): threadid=1: thread exiting with uncaught exception (group=0x40a621f8)
04-11 10:50:22.049: E/AndroidRuntime(17743): FATAL EXCEPTION: main
04-11 10:50:22.049: E/AndroidRuntime(17743): java.lang.NullPointerException
04-11 10:50:22.049: E/AndroidRuntime(17743):    at com.andri.hilltest.MainActivity.onClick(MainActivity.java:52)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at android.view.View.performClick(View.java:3511)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at android.view.View$PerformClick.run(View.java:14105)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at android.os.Handler.handleCallback(Handler.java:605)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at android.os.Looper.loop(Looper.java:137)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at android.app.ActivityThread.main(ActivityThread.java:4609)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-11 10:50:22.049: E/AndroidRuntime(17743):    at dalvik.system.NativeStart.main(Native Method)
04-11 10:55:22.109: I/Process(17743): Sending signal. PID: 17743 SIG: 9

I don't know what's goin on this code, I hope someone out there could help to solve my problem. I really do hope.... Thanks for your attention... Terima kasih.

2
  • you forget to initialize angka_array Array. Commented Apr 11, 2014 at 4:07
  • I've initialized it at the beginning of the code, but I haven't declare the length of the array... that's the main problem... Commented Apr 11, 2014 at 4:40

3 Answers 3

5

You forgot to initialize array_angka

// setting size to this scoping to just those 2 line of code
array_angka = new int[array_huruf.length];
Sign up to request clarification or add additional context in comments.

1 Comment

hope you understood problem as well
1

If you do not want to specify the length of the array then you will have to use a list...

ArrayList<Integer> array_angka = new ArrayList<Integer>();

You will then add to it as follows

array_angka.add(posisi_huruf);

You can retrieve from your ArrayList as follows

array_angka.get(0);

Remember if you do not know ahead of time the length of the array you are recreating the entire array every time you increase the size of the array, this is what ArrayList is for to give you a dynamically growing Array.

1 Comment

I'll get your answer as my reference in the future.. it also helps indeed... thank you ....
0

You need to initialize array_angka to an object before using it. It points to null by default.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.