Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Whats wrong with my java code? I really need help!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my java code? I really need help!

    So i was working on my code for a game called Minecraft and so here is my first code, titled mod_MOO.java.
    package net.minecraft.src;
     
    public class mod_MOO extends BaseMod
    {
            public static final Block MOOBlock = new MOO(123, ModLoader.addOverride("/terrain.png", "/MOO/MOOBlock.png").setHardness(2F).setResistance(1F).setStepSound(Block.soundStoneFootstep).setBlockName("MOO");
     
            public mod_MOO()
            {
     
            }
     
            public void load()
            {
                    ModLoader.RegisterBlock(MOOBlock);
                    ModLoader.AddName(MOOBlock, "MOO");
                    ModLoader.AddShapelessRecipe(new ItemStack(MOOBlock, 123), new Object[] {Item.leather, Item.leather});
            }
     
            public String getVersion()
            {
                    return "1.0.0";
            }
    }

    And now here is MOO.java:
    package net.minecraft.src;
     
    public class MOOBlock extends Block
    {
            public MOOBlock(int i, int j)
            {
                    super(i, j, Material.rock);
            }
    }

    The error is on mod_MOO.java, it says on eclipse that the line with all of the properties and ids that it is missing two )'s.

    Please help!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    Please copy and paste here the full text of the error messages that you get from the compiler.

    Some editors have tools for finding the ending char of an pair of () or {}.
    Put the cursor on the leading ( or { and press a key combo to find the pairing ) or }.
    In my editor its ctrl+ ]

    Start on the inside and work outwards to find where there is a missing one.
    Last edited by Norm; February 18th, 2012 at 07:04 AM.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    Please copy and paste here the full text of the error messages that you get from the compiler.
    heres what it says on eclipse in the console thing when I run it:
    27 achievements
    174 recipes
    ModLoader 1.1 Initializing...
    Failed to load mod from "mod_MOO.class"
    Done.
    Loading: net.java.games.input.OSXEnvironmentPlugin
     
    Starting up SoundSystem...
    Initializing LWJGL OpenAL
        (The LWJGL binding of OpenAL.  For more information, see http://www.lwjgl.org)
    OpenAL initialized.

    And heres what it says in Minecraft (when it crashes):
    Mods loaded: 1
    ModLoader 1.1
     
          Minecraft has crashed!      
          ----------------------      
     
    Minecraft has stopped running because it encountered a problem.
     
    If you wish to report this, please copy this entire text and email it to support@mojang.com.
    Please include a description of what you did when the error occured.
     
     
     
    --- BEGIN ERROR REPORT 9c4f49b2 --------
    Generated 2/18/12 7:04 AM
     
    Minecraft: Minecraft 1.1
    OS: Mac OS X (x86_64) version 10.6.8
    Java: 1.6.0_29, Apple Inc.
    VM: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Apple Inc.
    LWJGL: 2.4.2
    OpenGL: NVIDIA GeForce 9400M OpenGL Engine version 2.1 NVIDIA-1.6.36, NVIDIA Corporation
     
    java.lang.Error: Unresolved compilation problem: 
    	Syntax error, insert ")" to complete VariableInitializer
     
    	at net.minecraft.src.mod_MOO.<init>(mod_MOO.java:5)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    	at java.lang.Class.newInstance0(Class.java:355)
    	at java.lang.Class.newInstance(Class.java:308)
    	at net.minecraft.src.ModLoader.addMod(ModLoader.java:234)
    	at net.minecraft.src.ModLoader.readFromClassPath(ModLoader.java:1217)
    	at net.minecraft.src.ModLoader.init(ModLoader.java:708)
    	at net.minecraft.src.ModLoader.AddAllRenderers(ModLoader.java:150)
    	at net.minecraft.src.RenderManager.<init>(RenderManager.java:78)
    	at net.minecraft.src.RenderManager.<clinit>(RenderManager.java:9)
    	at net.minecraft.client.Minecraft.startGame(Minecraft.java:316)
    	at net.minecraft.client.Minecraft.run(Minecraft.java:620)
    	at java.lang.Thread.run(Thread.java:680)
    --- END ERROR REPORT 59e591c3 ----------

    And also, I cant seem to find any command (im using a mac and eclipse)
    Last edited by matthewpipie; February 18th, 2012 at 07:11 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    If your editor does not have the pair finding tool, you can use your eyeballs.
    Edit the line in question and put each section up to an ending ) on a line by itself and scan the lines for the correct pairing. Put the crusor after a ) and press Enter to move to a new line
    An example line:
    .setHardness(2F)

    Then scan the lines and pair the ( with their ending )s

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    I've found that the parenthesis after "MOO" is underlined red, and the one matching with it (before "MOO") is not underlined, so now I am just confused.
    Please don't get too complicated, Im very new to java

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    This is not complicated. You need to find where the missing ) is. Starting on the left find a ( and then move to the right end and find its pairing ).

    Break your long statement up into a bunch of smaller simpler statements instead of trying to do it all in one statement.
    For example:
    <type1> var1 = <value for var1>
    <type2> var2 = <value for var2>
    public static final Block MOOBlock = new MOO(var1, var2) ;

    If this is too complicated, print the program on a piece of paper and use a pencil to circle the ( and draw a line to the ending ) and circle it and connect the two. Continue until all (s are paired with a ).

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    for the "new MOO(123," there is no closing parenthesis, but everywhere I try on that line will give me another error.

    Otherwise, everything matches up!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    for the "new MOO(123," there is no closing parenthesis
    Add the ending ) at the just before the ;
    Break the statement up into several short simple statements like I suggested before.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    Add the ending ) at the just before the ;
    What it already is: setBlockName("MOO"); and when i do this: setBlockName("MOO")); basically the whole line goes crazy
    I separated each property thing into its own separate line, and it still says (on .setBlockName) theres an error
    heres my whole thing now:
    package net.minecraft.src;
     
    public class mod_MOO extends BaseMod
    {
            public static final Block MOOBlock = new MOO(120,
            ModLoader.addOverride("/terrain.png","/MOO/MOOBlock.png"),
            setHardness(2F),
            setResistance(1F),
            setStepSound(Block.soundStoneFootstep),
            setBlockName("MOO");
     
            public mod_MOO()
            {
     
            }
     
            public void load()
            {
                    ModLoader.RegisterBlock(MOOBlock);
                    ModLoader.AddName(MOOBlock, "MOO");
                    ModLoader.AddShapelessRecipe(new ItemStack(MOOBlock, 123), new Object[] {Item.leather, Item.leather});
            }
     
            public String getVersion()
            {
                    return "1.0.0";
            }
    }

    NOTE: it says this in the line with the setBlockName thing:

    Multiple markers at this line
    	- Syntax error, insert ")" to complete 
    	 VariableInitializer
    And again, the ) next to "MOO" is underlined red

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    The chained code that you wrote used .s to connect the value returned by one method to connect to the next method. You have left them out when you put the method calls on separate lines. Put the .s back where they go.

    Where did you copy this statement from? It is a classical example of how to confuse beginners.

    Go back to where you copied the statement from and copy it again.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    this is what i was supposed to type(replacing some things with MOO and MOOBlock):
            public static final Block oreTitanium = new CamelOreBlockOre(123, ModLoader.addOverride("/terrain.png", "/CamelMod/CamelOre/terrain/titaniumore.png").setHardness(3F).setResistance(5F).setStepSound(Block.soundStoneFootstep).setBlockName("oreTitanium");

    and now this is what mine says:
    package net.minecraft.src;
     
    public class mod_MOO extends BaseMod
    {
        public static final Block MOOBlock= new MOO(123, ModLoader.addOverride("/terrain.png", "/MOO/MOOBlock.png").setHardness(3F).setResistance(5F).setStepSound(Block.soundStoneFootstep).setBlockName("MOO");
     
            public mod_MOO()
            {
     
            }
     
            public void load()
            {
                    ModLoader.RegisterBlock(MOOBlock);
                    ModLoader.AddName(MOOBlock, "MOO");
                    ModLoader.AddShapelessRecipe(new ItemStack(MOOBlock, 123), new Object[] {Item.leather, Item.leather});
            }
     
            public String getVersion()
            {
                    return "1.0.0";
            }
    }

    yet it still gives an error on that same line on that same closing parenthesis

    Multiple markers at this line
    	- Syntax error, insert ")" to complete 
    	 VariableInitializer
    	- Syntax error, insert ")" to complete 
    	 VariableInitializer

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    Simplify your code removing the following from the constructor call.
    .setHardness(3F).setResistance(5F).setStepSound(Bl ock.soundStoneFootstep).setBlockName("MOO")
    Do those method calls one by one.

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    Quote Originally Posted by Norm View Post
    Simplify your code removing the following from the constructor call.

    Do those method calls one by one.
    like this?

    package net.minecraft.src;
     
    public class mod_MOO extends BaseMod
    {
        public static final Block MOOBlock = new MOO(123, ModLoader.addOverride("/terrain.png", "/MOO/MOOBlock.png")
        		.setHardness(3F)
        		.setResistance(5F)
        		.setStepSound(Block.soundStoneFootstep)
        		.setBlockName("MOO");
     
            public mod_MOO()
            {
     
            }
     
            public void load()
            {
                    ModLoader.RegisterBlock(MOOBlock);
                    ModLoader.AddName(MOOBlock, "MOO");
                    ModLoader.AddShapelessRecipe(new ItemStack(MOOBlock, 123), new Object[] {Item.leather, Item.leather});
            }
     
            public String getVersion()
            {
                    return "1.0.0";
            }
    }
    because theres still errors

  14. #14
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    can you try this with eclipse and see if it gives you an error? Or will that not work?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    What parameters does the MOO class's constructor take?
    Code the call to that constructor with single variables for each parameter. Remove all the rest of the method calls from the constructor.
    For example:
     public static final Block MOOBlock = new MOO(123, <a variable here>);

    Then before the constructor, define a variable for <a variable here> and use it in the constructor call above.

  16. #16
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    Quote Originally Posted by Norm View Post
    What parameters does the MOO class's constructor take?
    Code the call to that constructor with single variables for each parameter. Remove all the rest of the method calls from the constructor.
    For example:
     public static final Block MOOBlock = new MOO(123, <a variable here>);

    Then before the constructor, define a variable for <a variable here> and use it in the constructor call above.
    So heres my new mod_MOO:
    package net.minecraft.src;
     
    public class mod_MOO extends BaseMod
    {
        public static final Block MOOBlock = new MOO(123, ModLoader.addOverride("/terrain.png","/MOO/MOOBlock.png").setHardness(3F).setResistance(5F).setStepSound(Block.soundStoneFootstep).setBlockName("MOO"));
     
            public mod_MOO()
            {
     
            }
     
            public void load()
            {
                    ModLoader.RegisterBlock(MOOBlock);
                    ModLoader.AddName(MOOBlock, "MOO");
                    ModLoader.AddShapelessRecipe(new ItemStack(MOOBlock, 123), new Object[] {Item.leather, Item.leather});
            }
     
            public String getVersion()
            {
                    return "1.0.0";
            }
    }
    Now it has
    ModLoader.addOverride("/terrain.png","/MOO/MOOBlock.png").setHardness(3F)
    underlined red and the reason:
    "
    Cannot invoke setHardness(float) on the primitive type
    int
    "
    And heres my MOO.java, if you need it:
    package net.minecraft.src;
     
    public class MOO extends Block
    {
            public MOO(int i, int j)
            {
                    super(i, j, Material.rock);
            }
    }

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    If the MOO constructor takes 2 int variables, Change the code to something like this:
    int secParm = ModLoader.addOverride("/terrain.png","/MOO/MOOBlock.png").setHardness(3F).setResistance(5F).setStepSound(Block.soundStoneFootstep).setBlockName("MOO");
     
    public static final Block MOOBlock = new MOO(123, secParm);
    Then fix the value of secParm to be valid for the compile.

  18. #18
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my java code? I really need help!

    Quote Originally Posted by Norm View Post
    If the MOO constructor takes 2 int variables, Change the code to something like this:
    int secParm = ModLoader.addOverride("/terrain.png","/MOO/MOOBlock.png").setHardness(3F).setResistance(5F).setStepSound(Block.soundStoneFootstep).setBlockName("MOO");
     
    public static final Block MOOBlock = new MOO(123, secParm);
    Then fix the value of secParm to be valid for the compile.
    Now heres what it says:
    Cannot invoke setHardness(float) on the primitive type int
    and my code:
    package net.minecraft.src;
     
    public class mod_MOO extends BaseMod
    {
    	static int MOOthing = ModLoader.addOverride("/terrain.png","/MOO/MOOBlock.png").setHardness(3F).setResistance(5F).setStepSound(Block.soundStoneFootstep).setBlockName("MOO");
    	public static final Block MOOBlock = new MOO(123, MOOthing);
     
            public mod_MOO()
            {
     
            }
     
            public void load()
            {
                    ModLoader.RegisterBlock(MOOBlock);
                    ModLoader.AddName(MOOBlock, "MOO");
                    ModLoader.AddShapelessRecipe(new ItemStack(MOOBlock, 123), new Object[] {Item.leather, Item.leather});
            }
     
            public String getVersion()
            {
                    return "1.0.0";
            }
    }
    and the underlined red thing is this:
    ModLoader.addOverride("/terrain.png","/MOO/MOOBlock.png").setHardness(3F)

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Whats wrong with my java code? I really need help!

    You need to read the documentation for all the methods in that statement and see what they do, what values they return etc.
    As I said before you need to break that chain of method calls up into single statements. Something like this:
    <datatype> val = <objectRef>.<the method>(<the methods args>);

    Where you replace the values in <>with the values from the chained statement being broken up.

Similar Threads

  1. Whats wrong with my code?
    By Bryan29 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 5th, 2011, 09:12 AM
  2. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  3. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  4. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 04:34 PM
  5. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 07:38 PM

Tags for this Thread