Results 1 to 11 of 11
  1. #1
    JoeHill is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Simple but frustrating error

    //NPCHandler.java

    First this calls for luckMethod


    if(npcs[NPCID].npcType == 1589) {
    luckMethod(npcs[NPCID].npcType, "BabyRed");
    }


    //And here's luckMethod:
    public void luckMethod(int NPCID, String stringName) { //Joe HIll's NpcDrops


    int dropLuck = misc.random2(1000);
    String VC = (stringName)+"VC1";
    String C = (stringName)+"C1";
    String U = (stringName)+"U1";
    String R = (stringName)+"R1";
    String SR = (stringName)+"SR1";
    String MR = (stringName)+"MR1";
    String UR = (stringName)+"UR1";
    String Y = (stringName)+"Y1";

    System.out.println(VC);

    if (dropLuck <= 333) {
    ItemHandler.addItem(drops.VC(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); }

    else if (dropLuck <= 167) {
    ItemHandler.addItem(drops.C(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); System.out.println(dropLuck);}

    else if (dropLuck <= 50) {
    ItemHandler.addItem(drops.U(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); System.out.println(dropLuck);}

    else if (dropLuck <= 22) {
    ItemHandler.addItem(drops.R(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); System.out.println(dropLuck);}

    else if (dropLuck <= 14) {
    ItemHandler.addItem(drops.SR(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); System.out.println(dropLuck);}

    else if (dropLuck <= 7) {
    ItemHandler.addItem(drops.MG(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); System.out.println(dropLuck);}

    else if (dropLuck <= 3) {
    ItemHandler.addItem(drops.UR(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); System.out.println(dropLuck);}

    else if (dropLuck <= 1) {
    ItemHandler.addItem(drops.Y(), npcs[NPCID].absX, npcs[NPCID].absY, 1, GetNpcKiller(NPCID), false); System.out.println(dropLuck);}
    }









    //Drops.java
    public static int BabyRedVC[] = {534, 1749};
    public static int BabyRedVC1(){return BabyRedVC[(int)(Math.random()*BabyRedVC.length)];
    }


    public static int BabyRedC[] = {};
    public static int BabyRedC1(){return BabyRedC[(int)(Math.random()*BabyRedC.length)];
    }



    public static int BabyRedU[] = {2489, 2495, 2501, 2507};
    public static int BabyRedU1(){return BabyRedU[(int)(Math.random()*BabyRedU.length)];
    }



    public static int BabyRedR[] = {2678, 1155};
    public static int BabyRedR1(){return BabyRedR[(int)(Math.random()*BabyRedR.length)];
    }


    public static int BabyRedSR[] = {4587};
    public static int BabyRedSR1(){return BabyRedSR[(int)(Math.random()*BabyRedSR.length)];
    }


    public static int BabyRedMR[] = {7158};
    public static int BabyRedMR1(){return BabyRedMR[(int)(Math.random()*BabyRedMR.length)];
    }


    public static int BabyRedUR[] = {3140};
    public static int BabyRedUR1(){return BabyRedUR[(int)(Math.random()*BabyRedUR.length)];
    }


    public static int BabyRedY[] = {};
    public static int BabyRedY1(){return BabyRedY[(int)(Math.random()*BabyRedY.length)];
    }

    Okay here's my issue

    The parts in luckMethod where it says like,
    ItemHandler.addItem(drops.VC(),
    ItemHandler.addItem(drops.C(),
    ItemHandler.addItem(drops.U(),

    I need the compiler to read it as:
    ItemHandler.addItem(drops.BabyRedVC1() .....
    ItemHandler.addItem(drops.BabyRedC1() .....

    But it's going into Drops.java to find BabyRedVC1, so how can I tell it through NPCHandler that that is what it's looking for!?

    I appended VC1 and the others at the end of stringName so if I were to print out (VC) it would say - BabyRedVC1

    Instead luckMethod is looking for VC is drops.java
    Last edited by JoeHill; 06-01-2011 at 08:46 PM.

  2. #2
    JoeHill is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    The issue is on a much larger scale I tried to make a more simplified version but I set it up all wrong haha

    One min please

    FIXED
    Last edited by JoeHill; 06-01-2011 at 08:05 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    None of that makes sense.
    What is file1?
    Whay are you doing this?
    Java Code:
    int var = var+"1";
    Which clearly doesn't compile.
    What does this mean?
    Java Code:
    addItem(file1.var());
    var is an int (apparently), or is there also a method called var?

    What are you trying to do, and what code (real code that shows your problem) have you got?
    Because at the moment you haven't told us enough to provide any help at all.

  4. #4
    JoeHill is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    None of that makes sense.
    What is file1?
    Whay are you doing this?
    Java Code:
    int var = var+"1";
    Which clearly doesn't compile.
    What does this mean?
    Java Code:
    addItem(file1.var());
    var is an int (apparently), or is there also a method called var?

    What are you trying to do, and what code (real code that shows your problem) have you got?
    Because at the moment you haven't told us enough to provide any help at all.


    Var is a String -
    There is a method in file1 called Var1

    I fixed the first post, check it now please.

    You don't want me to post the full problem it's over 750 lines of code.

    I'd rather post what I have posted, and get a simple generic answer.
    Last edited by JoeHill; 06-01-2011 at 08:07 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Look at the code you posted and play compiler. What does the compiler see? What is the syntax of a method call?
    Java doesn't have a macro facility where it generates code at compile time. (generics excepted) The code that is compiled is the same as the code that you typed in.

  6. #6
    JoeHill is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Look at the code you posted and play compiler. What does the compiler see? What is the syntax of a method call?
    Java doesn't have a macro facility where it generates code at compile time. (generics excepted) The code that is compiled is the same as the code that you typed in.
    I understand that and it doesn't answer my question haha


    I think what I'm looking for is Reflection - which I've never used before.

    If someone could offer some help on that it'd be swell.
    Last edited by JoeHill; 06-01-2011 at 08:57 PM.

  7. #7
    JoeHill is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    yep .

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Search the forum or Google it.

    You've changed the original simple piece of code. Can you post a simple piece of code to show what you want to do?

    Is this what you want to do:
    Call a method in a class where the name of the method is contained in a variable.

    Go to this site, search for Reflection and the look at invoking a method:
    http://download.oracle.com/javase/tu...ybigindex.html
    Last edited by Norm; 06-01-2011 at 09:50 PM.

  9. #9
    JoeHill is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Call a method in a class where the name of the method is contained in a variable.
    Ah yes I've been trying to figure out how to word it - I was looking at the problem differently.


    Thanks that helps.

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

    Default

    Quote Originally Posted by JoeHill View Post
    Var is a String -
    There is a method in file1 called Var1
    I've heard of people deliberately obfuscating code by changing all the identifiers to emotionally loaded words like "God", "Superman", etc. It becomes impossible to read, because the mind can't separate the meanings of the names from what the identifiers represent in the code.

    Naming your variables utterly meaningless things like "Var1" is almost as bad...
    Get in the habit of using standard Java naming conventions!

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I think you have a modelling flaw here.
    I really suspect reflection is a poor choice to solve this problem and is an attempt to get around a deeper mistake.

    What is that code supposed to be for?
    What is it modelling?

    You have a load of hardcoded arrays with ids in them, and you're randomly picking an entry from those arrays.
    You have a method for each array.
    Those methods do exactly the same thing to exactly the same sort of object (an int[]).

Similar Threads

  1. 30 line simple gui, simple error, need help
    By cc11rocks in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 09:33 PM
  2. very frustrating.. recursive
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 01-06-2011, 11:25 PM
  3. get index from an array (very frustrating)
    By Yakg in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 07:26 PM
  4. Please help setting up Tomcat. Very frustrating.
    By wombatvvv in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 09-12-2010, 06:56 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
  •