Results 1 to 3 of 3
  1. #1
    mac666 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Simple "Global" function to return random int

    Hello
    I want to make a function that returns a random Int.
    I would like to call it by simply using
    myRandomedInt = rand(10);

    rand int is a public static int inside a class.
    This doesnt not work, anyone? how can i make a global function to return int? :/

    This is what i have:

    Java Code:
    public class RandomInt {
      public static int Random(int max){
    	  Random randomGenerator = new Random();
    	  int randomInt = randomGenerator.nextInt(max);
    	  return randomInt;
      }
    }
    and is called from another class with RandomInt.Randoms(10)
    But i get a "nullpointerexeption" when i try :/
    Thank you for reading!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Simple "Global" function to return random int

    I would like to call it by simply using
    You can not call a method NOT in the current class without specifying a class reference.
    You would need something like this:
    myRandomedInt = TheClass.rand(10);

    But i get a "nullpointerexeption"
    Please post the full text of the error message and the code that caused it.

  3. #3
    mac666 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Simple "Global" function to return random int

    Thank you. Im new to java you see,
    seems as my class / function did work, but i had another reference that made this error. (Im coding java to android)
    Thank you Norm!

Similar Threads

  1. Simple "Refresh" and "Forward" buttons
    By needSolution in forum AWT / Swing
    Replies: 3
    Last Post: 12-12-2011, 01:15 AM
  2. Replies: 3
    Last Post: 07-27-2011, 01:07 PM
  3. Replies: 3
    Last Post: 04-29-2011, 01:08 AM
  4. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 12:46 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •