Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Why does this always return null?

  1. #1
    ocomobock is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Why does this always return null?

    It's a little hard to explain what I'm doing so I'll post the code first in separate methods:

    Java Code:
    //This gets a random itemstack depending on the player's amount of votes.
    	public ItemStack get(Player p)
    	{
    		OcoListener v = new OcoListener();
    		if (v.votes.get(p.getName()) >= 1 && v.votes.get(p.getName()) <= 5)
    		{
    			Random r = new Random();
    			int i = r.nextInt(5);
    			if (i == 0)
    			{
    				return new ItemStack(Material.COOKED_BEEF, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 1)
    			{
    				return new ItemStack(Material.TNT, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 2)
    			{
    				return new ItemStack(Material.ROTTEN_FLESH, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 3)
    			{
    				return new ItemStack(Material.FLINT, new OcoListener().getAmount(this.get(p)));
    			}else
    			{
    				return new ItemStack(Material.TORCH, new OcoListener().getAmount(this.get(p)));
    			}
    		}else if (v.votes.get(p.getName()) >= 5 && v.votes.get(p.getName()) <= 15)
    		{
    			Random r = new Random();
    			int i = r.nextInt(10);
    			if (i == 0)
    			{
    				return new ItemStack(Material.COOKED_BEEF, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 1)
    			{
    				return new ItemStack(Material.TNT, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 2)
    			{
    				return new ItemStack(Material.ROTTEN_FLESH, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 3)
    			{
    				return new ItemStack(Material.FLINT, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 4)
    			{
    				return new ItemStack(Material.TORCH, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 5)
    			{
    				return new ItemStack(Material.APPLE, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 6)
    			{
    				return new ItemStack(Material.CLAY, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 7)
    			{
    				return new ItemStack(Material.BUCKET, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 8)
    			{
    				return new ItemStack(Material.GHAST_TEAR, new OcoListener().getAmount(this.get(p)));
    			}else
    			{
    				return new ItemStack(Material.STRING, new OcoListener().getAmount(this.get(p)));
    			}
    		}else
    		{
    			Random r = new Random();
    			int i = r.nextInt(20);
    			if (i == 0)
    			{
    				return new ItemStack(Material.COOKED_BEEF, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 1)
    			{
    				return new ItemStack(Material.TNT, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 2)
    			{
    				return new ItemStack(Material.ROTTEN_FLESH, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 3)
    			{
    				return new ItemStack(Material.FLINT, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 4)
    			{
    				return new ItemStack(Material.TORCH, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 5)
    			{
    				return new ItemStack(Material.APPLE, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 6)
    			{
    				return new ItemStack(Material.CLAY, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 7)
    			{
    				return new ItemStack(Material.BUCKET, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 8)
    			{
    				return new ItemStack(Material.GHAST_TEAR, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 9)
    			{
    				return new ItemStack(Material.COCOA, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 10)
    			{
    				return new ItemStack(Material.BOOK, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 11)
    			{
    				return new ItemStack(Material.BLAZE_ROD, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 12)
    			{
    				return new ItemStack(Material.EMERALD, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 13)
    			{
    				return new ItemStack(Material.ARROW, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 14)
    			{
    				return new ItemStack(Material.BREAD, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 15)
    			{
    				return new ItemStack(Material.EGG, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 16)
    			{
    				return new ItemStack(Material.WOOD, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 17)
    			{
    				return new ItemStack(Material.CAKE, new OcoListener().getAmount(this.get(p)));
    			}else if (i == 18)
    			{
    				return new ItemStack(Material.BLAZE_POWDER, new OcoListener().getAmount(this.get(p)));
    			}else
    			{
    				return new ItemStack(Material.REDSTONE, new OcoListener().getAmount(this.get(p)));
    			}
    		}
    	}
    Java Code:
    //This is used for setting the quantity of the itemstack depending on its type.
    	public int getAmount(ItemStack i)
    	{
    		if (i.equals(new ItemStack(Material.COOKED_BEEF)))
    		{
    			return 12;
    		}else if (i.equals(new ItemStack(Material.TNT)))
    		{
    			return 32;
    		}else if (i.equals(new ItemStack(Material.ROTTEN_FLESH)))
    		{
    			return 24;
    		}else if (i.equals(new ItemStack(Material.FLINT)))
    		{
    			return 32;
    		}else if (i.equals(new ItemStack(Material.TORCH)))
    		{
    			return 64;
    		}else if (i.equals(new ItemStack(Material.APPLE)))
    		{
    			return 16;
    		}else if (i.equals(new ItemStack(Material.CLAY)))
    		{
    			return 32;
    		}else if (i.equals(new ItemStack(Material.BUCKET)))
    		{
    			return 3;
    		}else if (i.equals(new ItemStack(Material.GHAST_TEAR)))
    		{
    			return 4;
    		}else if (i.equals(new ItemStack(Material.COCOA)))
    		{
    			return 32;
    		}else if (i.equals(new ItemStack(Material.BOOK)))
    		{
    			return 64;
    		}else if (i.equals(new ItemStack(Material.BLAZE_ROD)))
    		{
    			return 6;
    		}else if (i.equals(new ItemStack(Material.EMERALD)))
    		{
    			return 4;
    		}else if (i.equals(new ItemStack(Material.ARROW)))
    		{
    			return 32;
    		}else if (i.equals(new ItemStack(Material.BREAD)))
    		{
    			return 12;
    		}else if (i.equals(new ItemStack(Material.EGG)))
    		{
    			return 8;
    		}else if (i.equals(new ItemStack(Material.WOOD)))
    		{
    			return 64;
    		}else if (i.equals(new ItemStack(Material.CAKE)))
    		{
    			return 1;
    		}else if (i.equals(new ItemStack(Material.BLAZE_POWDER)))
    		{
    			return 6;
    		}else
    		{
    			return 64;
    		}
    	}
    This isn't as important:
    Java Code:
    //This method is used for getting the amount of times the for loop will run in the voteMade method above.
    	public int getTimes(Player p)
    	{
    		OcoListener v = new OcoListener();
    		if (v.votes.get(p.getName()) >= 1 && v.votes.get(p.getName()) <= 5)
    		{
    			return 2;
    		}else if (v.votes.get(p.getName()) >= 5 && v.votes.get(p.getName()) <= 10)
    		{
    			return 3;
    		}else
    		{
    			return 4;
    		}
    	}
    So basically, I'm trying to create a random itemstack depending on a player's votes. Each time it should generate a random item with a random amount, but it always returns null.

    Any idea why it would do that? I'm probably being stupid.

    If anyone wants to know, this is for when someone votes for the server and it sends a packet to it and gives them something for voting.

    v.votes.get(p.getName()) returns the amount of votes the player has, so the more times they vote the better things they get. But this happens whenever I try to make something similar to this:

    Java Code:
    int buySomething(Person guy)
    	{
    		if (guy.getMoney() > 250)
    		{
    			return guy.getMoney() - 250;
    		}else
    		{
    			return guy.getMoney();
    			guy.tell("You don't have enough money for that");
    		}
    	}
    Last edited by ocomobock; 06-17-2013 at 03:29 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Why does this always return null?

    What happened when you stepped through this with a debugger, or at least added some print statements to figure out what was going on? Where does the program's execution differ from what you expect?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ocomobock is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: Why does this always return null?

    Quote Originally Posted by KevinWorkman View Post
    What happened when you stepped through this with a debugger, or at least added some print statements to figure out what was going on? Where does the program's execution differ from what you expect?
    Well, here's the method that actually uses those variables:

    Java Code:
    public void voteMade(Vote vote)
    	{
            Bukkit.getServer().broadcastMessage("Test message before null check");
    		if (vote.getUsername() != null && vote != null)
    		{
    			//Log the info to the server in case it receives the vote
    			Bukkit.getServer().getLogger().log(Level.INFO, vote.getUsername() +" voted for the server");
    	        if (votes.get(vote.getUsername()) != null)
    	        {
    	        	//Add to the amount of votes the player has, if this player is in the hashmap
    	            votes.put(vote.getUsername(), votes.get(vote.getUsername())+1);
    	        }else
    	        {
    	        	//Puts the player in the hashmap if not
    	        	votes.put(vote.getUsername(), 1);
    	        }
    	        Player p = Bukkit.getPlayer(vote.getUsername());
    	        p.sendMessage(green+"Thank you for voting!");
    	        for (int i = 0; i < getTimes(p); i++)
    	        {
    		        p.getInventory().addItem(new OcoListener().get(p));
    	        }
    	        int exp = new Random().nextInt(50) + 50;
    	        p.giveExp(exp);
    	        Bukkit.getServer().broadcastMessage(blue+p.getName()+" voted for the server.");
    	        Bukkit.getServer().broadcastMessage(vote.getUsername());
    		}
    	}
    All the errors pretty much happen where I reference getTimes() and get(). Like I said, they always return null. I will try and get the error if you need to see that.
    Last edited by ocomobock; 06-17-2013 at 06:13 PM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Why does this always return null?

    I find it kind of hard to understand why this code does not end up in an endless loop to be honest (well endless until StackOverflow). You have a method get(Player) which calls a method this.get(Player) - in other words it calls itself recursively. Or am I missing something?

  5. #5
    ocomobock is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: Why does this always return null?

    Quote Originally Posted by gimbal2 View Post
    I find it kind of hard to understand why this code does not end up in an endless loop to be honest (well endless until StackOverflow). You have a method get(Player) which calls a method this.get(Player) - in other words it calls itself recursively. Or am I missing something?
    That's true I guess. I was trying to find a way to get the return value to get the quantity of items, so I did this in get():

    new OcoListener().getAmount(this.get(p)));

    Any idea how I would be able to do that without creating an endless loop?

    EDIT: I'm dumb, I could just use 'new OcoListener().getAmount(new ItemStack(Material.WHATEVER_MATERIAL));

    Anyways, I doubt that will stop it from the NullPointerException thing.
    Last edited by ocomobock; 06-17-2013 at 06:26 PM.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Why does this always return null?

    Quote Originally Posted by ocomobock View Post
    It's a little hard to explain what I'm doing . . .
    Honestly, that's probably the root of your problem. Before I start writing anything even moderately complex, I always write down in plain language what I intend for it to do. If I haven't thought it through enough to explain it, I haven't thought it through enough to code it.
    DarrylBurke likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. How to return a null result??
    By Yakg in forum New To Java
    Replies: 6
    Last Post: 12-07-2010, 08:38 PM
  2. image uploaded while name and city return null
    By mutago in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-24-2010, 06:52 AM
  3. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  4. getSession() Function Return Null value
    By dalchndr@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-04-2009, 07:24 AM
  5. return a null method
    By valoyivd in forum New To Java
    Replies: 2
    Last Post: 04-21-2008, 11:19 PM

Posting Permissions

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