aspose file tools*
The moose likes Beginning Java and the fly likes reading a private method within a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading a private method within a class" Watch "reading a private method within a class" New topic
Author

reading a private method within a class

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

I'm not sure why my code does not seem to run the readIcons() private method in the Deck class...



I would like to know whether CardIcons and sequentialCardIcons are being loaded with images, and, to unit test that the images are in order.

Any idea whether which of a two-dimensional array or a one dimensional array is more efficient in terms of memory and why? Am I using an appropriate data structure for storing images?

Some of the images can be found here.




Jon
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Hey Jon,

I am not sure about the data structure needed for images, but i can help with the readIcons() method.
You cant call instance methods from your constructor,so if you really want to call the method, youu need to qualify it with the static modifier!!!...



If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

jishnu dasgupta wrote:You cant call instance methods from your constructor,so if you really want to call the method, youu need to qualify it with the static modifier!!!...


This is completely incorrect.

I expect the code at line 88 (in your post) is not being executed because an exception is being thrown before control reaches that point. And since at line 91 you specifically wrote code to ignore all exceptions, you won't find that out very easily. So don't do that (ignore exceptions, I mean).

Jon Camilleri wrote:Any idea whether which of a two-dimensional array or a one dimensional array is more efficient in terms of memory and why?


I don't have any idea. But that's the wrong question anyway. How big is this array? If it isn't going to take many megabytes it doesn't matter in the least whether it takes 10,000 bytes or 12,000 bytes. Use whichever is easier to write the code for.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

My guess is that line 86 is throwing an exception because you never initialise CardIcons. Follow Paul's advice and you'll be able to confirm that.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Incidentally: Deck extends Card? A deck is-not-a card, so that's poor design that's going to cause problems at some point.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
[Pedantic mode]There's no such thing as a 2-D array. Only a 1-D array, of 1-D arrays[/pedantic mode]

Don't know whether an array or array of arrays is more efficient. I would think the difference is probably slight, and you ought not to worry about such considerations unless there are signs of slow performance.
You appear only to be calling that method from your constructor, and you have correctly labelled the method private. Any advice that you can't call instance methods from a constructor is mistaken. Does every overloaded constructor call that method? I presume you haven't commented out the call to the method, as you have in your 1-argument constructor.

You have got far too much code which you haven't tried. You ought to write about 5 lines, compile and run the code, and see what sort of errors you suffer. Then you will know the code is actually running.
Add some debugging code inside that method. Start like this:Add some code to print out the names of the files you are opening, so you can verify they are correct.
You have got an empty catch block: put something "real" in it, egThe break; instruction is obscuring any errors and exceptions, so you don't find out what is going wrong.
Never, never write == true or == false. Both are poor style and very error-prone.
You ought not to start identifiers with _ (except in package names).
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Arrrrrgh!!!........



Sorry Jon and thanks paul.....dont know how i made that stupid mistakes..........

.....Guess its time to go to sleep!!!.....
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Besides it was my 13th post..!!!...had to go wrong....
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

jishnu dasgupta wrote:Hey Jon,

I am not sure about the data structure needed for images, but i can help with the readIcons() method.
You cant call instance methods from your constructor,so if you really want to call the method, youu need to qualify it with the static modifier!!!...



I can't make readIcons() static, because it would crash the program anyway:


Error

Cannot make a static reference to the non-static field sequentialCardIcons

at Deck.readIcons(Deck.java:63)
at Deck.<init>(Deck.java:18)
at Poker.main(Poker.java:6)


Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Campbell Ritchie wrote:[Pedantic mode]There's no such thing as a 2-D array. Only a 1-D array, of 1-D arrays[/pedantic mode]

Don't know whether an array or array of arrays is more efficient. I would think the difference is probably slight, and you ought not to worry about such considerations unless there are signs of slow performance.
You appear only to be calling that method from your constructor, and you have correctly labelled the method private. Any advice that you can't call instance methods from a constructor is mistaken. Does every overloaded constructor call that method? I presume you haven't commented out the call to the method, as you have in your 1-argument constructor.

You have got far too much code which you haven't tried. You ought to write about 5 lines, compile and run the code, and see what sort of errors you suffer. Then you will know the code is actually running.
Add some debugging code inside that method. Start like this:Add some code to print out the names of the files you are opening, so you can verify they are correct.
You have got an empty catch block: put something "real" in it, egThe break; instruction is obscuring any errors and exceptions, so you don't find out what is going wrong.
Never, never write == true or == false. Both are poor style and very error-prone.
You ought not to start identifiers with _ (except in package names).


Thanks.

I could not get ex.printStackTrace() to work somehow, when trying to add the following lines to my exception handling block:


However, enabling the exceptions to be displayed resulted that the image files cannot be read, and, I still could not figure out

1. Why it can't read the GIF files from the directories?
2. How can I specify relative sub-directories (i.e. look for /img/01.GIF.. img/02.GIF up to img/52.gif. Not all of the image files exist at the moment?


Output
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\01.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\02.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\03.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\04.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\05.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\06.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\07.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\08.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\09.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\10.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\11.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\12.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\13.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\14.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\15.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\16.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\17.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\18.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\19.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\20.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\21.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\22.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\23.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\24.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\25.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\26.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\27.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\28.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\29.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\30.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\31.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\32.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\33.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\34.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\35.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\36.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\37.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\38.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\39.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\40.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\41.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\42.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\43.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\44.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\45.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\46.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\47.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\48.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\49.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\50.GIF
Can't read input file!
DEBUG: try to get filename
DEBUG: _filename = C:\Documents and Settings\Jon\workspace\Poker\src\img\51.GIF
Can't read input file!
[Ace of Clubs, Two of Clubs, Three of Clubs, Four of Clubs, Five of Clubs, Six of Clubs, Seven of Clubs, Eight of Clubs, Nine of Clubs, Ten of Clubs, Jack of Clubs, Queen of Clubs, King of Clubs, Ace of Diamonds, Two of Diamonds, Three of Diamonds, Four of Diamonds, Five of Diamonds, Six of Diamonds, Seven of Diamonds, Eight of Diamonds, Nine of Diamonds, Ten of Diamonds, Jack of Diamonds, Queen of Diamonds, King of Diamonds, Ace of Hearts, Two of Hearts, Three of Hearts, Four of Hearts, Five of Hearts, Six of Hearts, Seven of Hearts, Eight of Hearts, Nine of Hearts, Ten of Hearts, Jack of Hearts, Queen of Hearts, King of Hearts, Ace of Spades, Two of Spades, Three of Spades, Four of Spades, Five of Spades, Six of Spades, Seven of Spades, Eight of Spades, Nine of Spades, Ten of Spades, Jack of Spades, Queen of Spades, King of Spades]



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

The string "Can't read input file!" doesn't appear in the code you posted. Therefore that output isn't coming from the code you posted.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3571
    
  15
Paul Clapham wrote:The string "Can't read input file!" doesn't appear in the code you posted. Therefore that output isn't coming from the code you posted.

Yes it is. He followed your advice to not ignore exceptions and ImageIO.read() throws an IOException with that message.


Joanne
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Joanne Neal wrote:
Paul Clapham wrote:The string "Can't read input file!" doesn't appear in the code you posted. Therefore that output isn't coming from the code you posted.

Yes it is. He followed your advice to not ignore exceptions and ImageIO.read() throws an IOException with that message.


Really? That's a very un-traditional error message, I've never seen one like that. Anyway displaying the stacktrace (e.printStackTrace()) would be a better strategy.
Manjula Weerasinghe
Greenhorn

Joined: Mar 15, 2010
Posts: 25

add ".GIF" to the end of the line 54;
It should be


OCPJP 6, OCE JEE 6 JSP and Servlet Developer
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

From javax.imageio.ImageIO source code:



I guess they got their summer intern to write this one!
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Manjula Weerasinghe wrote:add ".GIF" to the end of the line 54;
It should be


Changing code to the following still results in an exception:

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Luigi Plinge wrote:From javax.imageio.ImageIO source code:



I guess they got their summer intern to write this one!


lol, well I asked Oracle just in case they can provide more information, and this seems to be a problem with storing Images in an array[], which is not possible, thus the error message is not clear enough to newbies like me.

Now I am using an ArrayList<Image> to store the images, and, although I still haven't coded the viewer, data seems to be going into the ArrayList, because I have walked through the code:



I'm not yet sure how to get to the subdirectory that is relative to the current directory, and, have a problem ensuring that the image icons have consistent dimensions in terms of pixels (e.g. all images to confirm to 75 x 100 pixels, because it's tough to do this manually).
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Jon Camilleri wrote:
Luigi Plinge wrote:From javax.imageio.ImageIO source code:



I guess they got their summer intern to write this one!


lol, well I asked Oracle just in case they can provide more information, and this seems to be a problem with storing Images in an array[], which is not possible, thus the error message is not clear enough to newbies like me.

Now I am using an ArrayList<Image> to store the images, and, although I still haven't coded the viewer, data seems to be going into the ArrayList, because I have walked through the code:



I still have a problem ensuring that the image icons have consistent dimensions in terms of pixels (e.g. all images to confirm to 75 x 100 pixels, because it's tough to do this manually), however this is not a big issue at this stage.

I am also wondering whether the line marked above is superfluous (copied and pasted from somewhere), because it does not seem to make sense to read into _image twice, and, the object seems to be getting a value even when I comment it out. However, an image reader is still in the works, so I can't be sure until I actually read my icons off getCardIcons().




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading a private method within a class