Sunday, August 10, 2014

NullPointerException at android.widget.ArrayAdapter.createViewFromResource


Crashes while using code like this:

AlertDialog.Builder builder = new AlertDialog.Builder(StatusMenuConflicts.this);
builder.setTitle("Star Traders RPG");
builder.setIcon(R.drawable.icon);
final String[] items = new String[6];
items[0] = getString(R.string.status);

The stack trace won't contain your code.

 java.lang.NullPointerException
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
        at android.widget.AbsListView.obtainView(AbsListView.java:1435)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1288)
        at android.widget.ListView.onMeasure(ListView.java:1199)
        at android.view.View.measure(View.java:8322)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
        at android.view.View.measure(View.java:8322)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
        at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
        at android.view.View.measure(View.java:8322)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.view.View.measure(View.java:8322)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.view.View.measure(View.java:8322)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3806)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)

The problem is that Android is looking for 6 elements in the Array you're passing in, but you've only defined one. Set the length of you String[] correctly and the code will work.

Friday, August 08, 2014

Interviews -- Reverse Singly Linked List, C++

Just seriously struggled through an interview on the phone. It's always the simple programming problems that seem the hardest when someone is watching you do it.

The question was "Write a simple function to reverse a linked list" and I couldn't get it right until 3 minutes after the interview was over. Every. Single. Time.

So here is the code I ended up with, given this basic Linked list.

struct List {
   int value;
   mySingleLinked* next;
};

Seems simple, right? See the comments ;)

// arg. i'm an idiot
List reverseLinkedList(List* head) {
    List *tempList, *nextList;

tempList = 0;
nextList = 0;

while (head != 0)
{
nextList = head->next;
head->next = tempList;
tempList = head;
head = nextList;
}
return tempList;
}