1

I am trying to access an array przezwiska located in data.java from my AlarmReciever. The array is used to access a random position from the array in order to generate a notification. I imported the data file, but after i compile it and run, i get java.lang.NullPointerException: Attempt to read from null array.

My AlarmReciever.java:

public void onReceive(Context context, Intent intent) {

    int x = (int) getRandomNumber();

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("My title text "+ data.przezwiska[x] + data.sercoweOczyStr)
            .setContentText("My content text " + data.smileyStr).setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{500, 250, 250, 250});
    notificationManager.notify(0, mNotifyBuilder.build());
}

data.java:

package com.example.palidon.myapplication.Data;

public class data {


public static int kotekSerce = 0x1F63B;
public static int slonce = 0x1F31E;
public static int sercoweOczy = 0x1F60D;
public static int serce = 0x1F49E;
public static int zloteSerce = 0x1F49B;
public static int smiley = 0x1F60A;


public static String kotekSerceStr = getEmojiByUnicode(kotekSerce);
public static String slonceStr = getEmojiByUnicode(slonce);
public static String sercoweOczyStr = getEmojiByUnicode(sercoweOczy);
public static String serceStr = getEmojiByUnicode(serce);
public static String smileyStr = getEmojiByUnicode(smiley);

public static String[] przezwiska;

public static void main(String[] args) {

    przezwiska = new String[20];
    przezwiska[0] = "kotku! " + kotekSerceStr;
    przezwiska[1] = "słońce! " + slonceStr;
    przezwiska[2] = "kochanie! " + serceStr;
    }
}

1 Answer 1

2

Change your main() method into a static initializer. This:

public static void main(String[] args) {

    przezwiska = new String[20];
    przezwiska[0] = "kotku! " + kotekSerceStr;
    przezwiska[1] = "słońce! " + slonceStr;
    przezwiska[2] = "kochanie! " + serceStr;
}

should become this:

static {
    przezwiska = new String[20];
    przezwiska[0] = "kotku! " + kotekSerceStr;
    przezwiska[1] = "słońce! " + slonceStr;
    przezwiska[2] = "kochanie! " + serceStr;
}

You need to do this because nothing about the Android system will call the main() method for you, so your array will never be initialized. A static initializer block, on the other hand, will be automatically called, so your array will be correctly initialized when you try to use it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanation. That solved my problem.

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.