YesNoOk
avatar

Help with Java (Read 1199 times)

Started by ~*Ishida-Uryuu*~, May 21, 2009, 11:00:51 am
Share this topic:
Help with Java
#1  May 21, 2009, 11:00:51 am
  • *****
  • DAT SAX
Hey guys, I've got this assignment for a class where I've got to make a game about a player trapped in a Dungeon maze with traps and all this other crap in Java, but I'm barely getting started. I'm also very beginner-ish in Java.

So far, I've self-resolved most of my issues, but now I'm having trouble with this array of objects and it's pissing me off. I can't figure out why it won't work. I'm trying to get dungeon.CellArrayTester(dungeon.populateGrid(r,c), choice) to pretty much return the same value as choice, which I want to be a selected dungeon cell's cell#.

I get this error:

Quote
I:\java\d_and_a\MainGame.java:56: CellArrayTester(Cells[],int) in DungeonMaze cannot be applied to (java.lang.Object[],int)
      System.out.println("\nCell " + choice + "is Cell #" + dungeon.CellArrayTester(dungeon.populateGrid(r,c), choice));
                                                                                          ^
1 error

Here's the problem line:

Spoiler: In the main method (click to see content)

Here's the populateGrid method in DungeonMaze.java:

Spoiler: populateGrid (very messy ATM, but works as desired) (click to see content)

Here's the CellArrayTester method in DungeonMaze.java:

Spoiler: CellArrayTester (click to see content)

and here's the whole Cells class:

Spoiler: Cells (click to see content)

If I'm not mistaken, populateGrid should return an Object array, which is what I thought it needed, but I'm not even sure if that's the problem. Any help here?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Last Edit: May 21, 2009, 11:17:44 am by ~*Ishida-Uryuu*~
Re: Help with Java
#2  May 21, 2009, 11:08:02 am
  • ******
    • Germany
    • valodim@gmail.com
[code=java] totally improves readability <- :)

It's the return type of your populate method :P
Re: Help with Java
#3  May 21, 2009, 11:18:26 am
  • *****
  • DAT SAX
Oh, cool, didn't know we had that type of code. :P Fixed.

What should the return type be to make it work?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Re: Help with Java
#4  May 21, 2009, 12:24:35 pm
  • ******
    • Germany
    • valodim@gmail.com
the main method provides a pretty good hint
Re: Help with Java
#5  May 21, 2009, 01:38:11 pm
  • *****
  • DAT SAX
Oh wow, I didn't know "classname[]" could be an object type. o_O

It worked ... for a while. Everything compiles fine, but I changed around a lot of stuff and now it keeps throwing this extremely annoying java.lang.NullPointerException error.

Here's the problem line in DungeonMaze.java (apparently, I never changed this line and it used to work):

Code: (java)
cell[numCells].setCellNumber(numCells);

The array of cells is instantiated here in StartGame.java:

Code: (java)
Cells[] cellArray = new Cells[r*c];

and reused in the populateGrid method in DungeonMaze.java here:

Code: (java)
public Cells[] populateGrid(int numRows, int numCols, Cells[] cell) {

Here's the entire re-made populateGrid method:

Spoiler, click to toggle visibilty

The method setCellNumber() in Cells.java is here, if important at all:

Code: (java)
public void setCellNumber(int number) {

this.cellNumber = number;

}


The SUCCESS thing is to see how many loops the for loop actually went though, and so far, it only prints out one, then it throws the error. :(

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Last Edit: May 21, 2009, 01:41:13 pm by ~*Ishida-Uryuu*~
Re: Help with Java
#6  May 21, 2009, 02:06:27 pm
  • ******
    • Germany
    • valodim@gmail.com
the
Stuff[] a = new Stuff[];
part only creates an array of Stuff[] references, not the objects themselves. you have to initialize them one by one to do that:
a[0] = new Stuff(lol);
Re: Help with Java
#7  May 21, 2009, 02:20:43 pm
  • *****
  • DAT SAX
Haha, how could I be so stupid. :XD: I thought what I did actually created the objects.

Thanks Val, it works perfectly now. :D I'll keep this thread open for any future problems, because this will be a difficult program for me, and I'll probably need a lot more help later on.

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Re: Help with Java
#8  May 21, 2009, 04:08:00 pm
  • ******
    • Germany
    • valodim@gmail.com
you might also want to make that a two-dimensional array, if you want to have a rows/columns model. makes a bunch of operations on those easier
Re: Help with Java
#9  May 25, 2009, 12:28:56 am
  • *****
  • DAT SAX
Well, for one I don't know how to make those yet, and I got some advice from a person experienced in the field, and he says those suck. :P Anyway, I had it working the way I wanted to for a little while, then the shit fell apart on me and said "FUCK YOU".

Don't feel like typing out another explanation, so here's a repost from another forum:

Quote
I'm making a game for a project due this Tuesday. You're in a dungeon with rows and columns, and for each coordinate there is a cell and there are 3 trap cells that you instantly die from if you walk into them. Also, you die if you hit 3 walls.

For debugging purposes, I worked out a grid printing system, and various things that tell you where the player is, cell #, cell coordinates, player coordinates, etc, so I know exactly where everything is. In the final version, there will be no grid and none of that.

Everything ATM, works how I want it to, except for ONE thing. If a trap or the player gets placed in Column 0, Row 4 its cell number gets increased by anywhere from 1 to 3, sharing a cell number with another one of the cells, and I have no idea why this happens. I've tried putting in countermeasures for this, but they don't seem to take effect.

I used to have a working system where it displayed on the grid the character's position every time I moved and all of the traps' positions, but now it only displays either if it's in the glitched cell.

Here is the entire program, 5 files zipped up in a .rar:

http://www.sendspace.com/file/t3rsxj

The problem files are DungeonMaze.java, Player.java and Cells.java. I apologize if it's messy. Also, ignore the various profanities placed about, it's from anger. They're not gonna be there when I turn this in. :P

I'm in desperate need of help, guys. I'm VERY beginner with Java (since our teacher "teaches" us jack squat) so there may be some very noobish mistakes. Anything is appreciated! Thank you!


                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Re: Help with Java
#10  May 25, 2009, 03:43:18 am
  • avatar
  • ******
    • USA
You, beginner-ish? I always thought of you as the guy who knew everything lol. Anyways, I think there is a tutorial on Newgrounds.com. I think theres one for both flash and java.
Re: Help with Java
#11  May 25, 2009, 02:33:17 pm
  • *****
  • DAT SAX
Ok, solved the last problem thanks to another forum.

Getting an exception here, though:

Code: (java)
			System.out.print("\nWhich direction would you like to move?: ");
String dir = input.nextLine();
char d = dir.charAt(0);
dir = Character.toString(d);

In the program, this prints out:

Which direction would you like to move?: Exception in thread "main" java.lang.StringIndexOutOfBounds
Exception: String index out of range: 0
        at java.lang.String.charAt(String.java:687)
        at Game.MoveCharacter(Game.java:71)
        at Game.StartGame(Game.java:47)
        at Main.main(Main.java:35)

No clue what's wrong. It doesn't seem to be waiting for my input before trying to set char d to its value.

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Re: Help with Java
#12  May 25, 2009, 02:35:22 pm
  • ******
  • does this look like the face of mercy?
print d and any other array movement , and print the total length of the array.
Re: Help with Java
#13  May 25, 2009, 02:54:49 pm
  • *****
  • DAT SAX
What exactly are you trying to say? Are you telling me to do that? If so, why?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Re: Help with Java
#14  May 25, 2009, 02:57:22 pm
  • ******
  • does this look like the face of mercy?
 java.lang.StringIndexOutOfBounds

The index is out of bounds, im telling you to do that so you can see which of the indexes is going over the size of the full thing.
Re: Help with Java
#15  May 25, 2009, 03:01:33 pm
  • *****
  • DAT SAX
I don't see what that will do. I'm not working with any actual arrays here for this part.

I'm just trying to set dir to a user-defined input of any length, then take the first letter and set dir to that, but it's like, skipping the String dir = input.nextLine() line, and going straight to the char d line.

It used to work as it is, but that was until I put this in the method that MoveCharacter() (that section of code's method) is called from:

Code: (java)
		System.out.print("\nSet mode difficulty (1: SUPER EASY - Grid display) (2: NIGHTMARE MODE - No grid display): ");
int grid = input.nextInt();

while(grid > 2 || grid < 1) {

System.out.print("Please enter either 1 or 2: ");
grid = input.nextInt();

}

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Re: Help with Java
#16  May 25, 2009, 03:13:18 pm
  • ******
  • does this look like the face of mercy?
I misread, thought that this
Code:
dir = Character.toString(d);
had [d] instead of (d)


I think it might be reading the empty line, and failing.Make a check to be sure that its not equal to null before attempting reading.
Re: Help with Java
#17  May 25, 2009, 04:12:33 pm
  • ******
  • YOMI
    • www.justnopoint.com/lbends
AH DUN TOLD YOU BOY.
AH TOLD YA TO GET RID OF SCANNER.
IT'S BAD NEWS.

(You can't ignore me this time because this is exactly where your problem is now)

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

You're welcome.

Spoiler, click to toggle visibilty
Last Edit: May 25, 2009, 04:22:17 pm by Insanius
QUICK JAVA QUESTION
#18  May 25, 2009, 11:00:33 pm
  • *****
  • DAT SAX
Gwehehehe >:]

Unnecessary! >:P:

Fixed it by adding input.nextLine(); before my MoveCharacter() call to clear the input buffer, and my program works flawlessly now. >:D

MISSHUN KUMPREET


EDIT: QUICK QUESTION!

How do I make an exe that runs the game so I can send it to people who don't have compilers?

                  ~*The Last Quincy*~         ~Sig by [TempesT]
My MUGEN shit:
MvC2 Tron Bonne, MvC Kim Kaphwan, Barns (Destruction Desire),
and Burai Yamamoto (Arranged)
Last Edit: May 25, 2009, 11:42:45 pm by ~*Ishida-Uryuu*~
Re: QUICK JAVA QUESTION
#19  May 26, 2009, 12:08:29 am
  • ******
    • Germany
    • valodim@gmail.com
How do I make an exe that runs the game so I can send it to people who don't have compilers?

..with java, you don't. that's why people have to download those runtimes. what you can create is a jar file, which is basically just an archive of the class files.. there are tutorials for these kinda very routine things, y'know ;P

and two-dimensional arrays work just like one-dimensional ones.. just with two dimensional quantifiers :P
ie:
Code: (java)
int[][] stuff = new int[3][3];
for(int i = 0; i < 9; i++)
  stuff[i%3][2-i%3] = i;

Insanius: What's wrong with a Scanner?
Re: Help with Java
#20  May 26, 2009, 12:52:12 am
  • ******
  • YOMI
    • www.justnopoint.com/lbends
Sometimes the Scanner will behave erratically
You have very little control over the amount of data it reads
Error handling is a nightmare
Re: Help with Java
#21  May 26, 2009, 08:22:04 am
  • ******
    • Germany
    • valodim@gmail.com
Huh. I don't agree.

..let's not discuss it though. :P