Null Pointer Exception when trying to use (java.util.Random) in method - beginner Java

Hoob

I'm currently trying to write a very simple program to test the capabilities of the "Random" class (java.util.Random) in a few ways, however I receive a Null Pointer Exception error when trying to run.

My code:

import java.util.Random;

public class RandomTester
{
    public Random r;

    public RandomTester()
    {
        Random r = new Random();
    }

    public void printOneRandom()
    {
        int s = 0;
        s = r.nextInt(256);  //NULL POINTER EXCEPTION HERE
        System.out.println("Random 8-bit number: " + s);

    }

    public void printMultiRandom(int howMany)
    {
        for(int i=0 ; i<howMany ; i++)
        {
            System.out.println("Random 8-bit number: " + r.nextInt(256));
        }    
    }

}

It works fine if I move the constructor from RandomTester() to either one of the methods but I was under the impression that writing it the above way should be okay. It may well just be I'm not quite grasping something fundamental or indeed that I should have instantiate the object elsewhere.

Thank you for any help, there's no main method shown as I'm using BlueJ (a learner IDE). It's also my first time exploring some of the basic classes.

Sotirios Delimanolis

Change this

Random r = new Random();

to

r = new Random();

You were previously initializing a different local variable that has a name that shadows your instance field. Your instance field was therefore initialized to a default value of null.

When you tried to use it in one of your methods, it was still null and gave you a NullPointException when you tried to dereference it to invoke a method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Null Pointer Exception when trying to use (java.util.Random) in method - beginner Java

From Dev

Null Pointer exception when trying to return HashMap value in java

From Dev

doFilter method on java - null pointer exception on filterchain when session is invalidate

From Dev

Null pointer exception when trying to access triangular array in method

From Dev

null pointer exception in java when coding

From Dev

null pointer exception in java .

From Dev

Null Pointer exception when trying to call getLayoutInflater

From Dev

Java Multithreading null pointer exception

From Dev

Null pointer exception in java ArrayList

From Dev

Android - Java null pointer exception

From Dev

JAVA: Null Pointer Exception error

From Dev

Java Multithreading null pointer exception

From Dev

java null pointer exception JMenuBar

From Dev

Weird Java Null Pointer Exception

From Dev

Null Pointer Exception (Java) [EDIT!]

From Dev

Errors when trying to use import java.util.Scanner

From Dev

Java: Null pointer when trying to set app icon

From Dev

Java, Null Pointer Exception. when I add an object to an array

From Dev

Java Null Pointer exception when querying SQLite Database in Android

From Dev

Java null pointer exception after checking if null

From Dev

Exception java.util.NoSuchElementException when trying to read text file using java.util.Scanner + write file into different matrix

From Dev

Null pointer exception trying to invoke onClick method from custom Adapter

From Dev

Contention in concurrent use of java.util.Random

From Dev

Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference [Android]

From Dev

Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference [Android]

From Dev

Trying to download files from S3 Bucket using Java SDK, Null pointer Exception on isStandardEndpoint

From Dev

Java Server Thread Null Pointer Exception error raised after trying to encode / decode strings into UTF-8

From Dev

Java Generic Object Null Pointer Check Throwing Null Pointer Exception

From Dev

Getting a Null Pointer Exception when I am trying to start PySpark

Related Related

  1. 1

    Null Pointer Exception when trying to use (java.util.Random) in method - beginner Java

  2. 2

    Null Pointer exception when trying to return HashMap value in java

  3. 3

    doFilter method on java - null pointer exception on filterchain when session is invalidate

  4. 4

    Null pointer exception when trying to access triangular array in method

  5. 5

    null pointer exception in java when coding

  6. 6

    null pointer exception in java .

  7. 7

    Null Pointer exception when trying to call getLayoutInflater

  8. 8

    Java Multithreading null pointer exception

  9. 9

    Null pointer exception in java ArrayList

  10. 10

    Android - Java null pointer exception

  11. 11

    JAVA: Null Pointer Exception error

  12. 12

    Java Multithreading null pointer exception

  13. 13

    java null pointer exception JMenuBar

  14. 14

    Weird Java Null Pointer Exception

  15. 15

    Null Pointer Exception (Java) [EDIT!]

  16. 16

    Errors when trying to use import java.util.Scanner

  17. 17

    Java: Null pointer when trying to set app icon

  18. 18

    Java, Null Pointer Exception. when I add an object to an array

  19. 19

    Java Null Pointer exception when querying SQLite Database in Android

  20. 20

    Java null pointer exception after checking if null

  21. 21

    Exception java.util.NoSuchElementException when trying to read text file using java.util.Scanner + write file into different matrix

  22. 22

    Null pointer exception trying to invoke onClick method from custom Adapter

  23. 23

    Contention in concurrent use of java.util.Random

  24. 24

    Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference [Android]

  25. 25

    Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference [Android]

  26. 26

    Trying to download files from S3 Bucket using Java SDK, Null pointer Exception on isStandardEndpoint

  27. 27

    Java Server Thread Null Pointer Exception error raised after trying to encode / decode strings into UTF-8

  28. 28

    Java Generic Object Null Pointer Check Throwing Null Pointer Exception

  29. 29

    Getting a Null Pointer Exception when I am trying to start PySpark

HotTag

Archive