• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems overwriting toString() method.

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my assignment is to create a deck class that holds 52 cards in it which obtain their values from a Card class that I wrote earlier. I am supposed to overwrite the toString method of java with one of my ownt that prints out a string with all 52 cards represented not by their numeric values as they are in my Card Class but rather as Strings. The card class works fine ( i know this because it has already been graded) and has two methods in it which convert the values assigned to each card into a value and suit accordingly. anyways this is my code so far but for some reason that I can't figure out i keep getting a runtime error of a null pointer exception



the Card.toString() method which i'm calling in the Deck.toString method is the problem I think but I can't figure out how to do it differently. What I need is for the Deck toString() method to print out all 52 cards in the yourdeck[] array but instead of printing out 1,1 for the ace of spades I need it to print out "ace of spades". If it will help I will post the card class which I wrote previously that contained the methods I used to convert an object of type card from int values into string values. Thank you for any help offered
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the output that you get from these exceptions. This will give us a starting place to help track down the cause.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p.s. You are trying to override the toString() method, not overwrite it
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The loop conditions here leave the entire yourDeck array full of null references. Did the grader run the program, or is this a post-grading typo.

The condition (middle expression between semicolons) must test true before entering each loop iteration. Note that if you just reverse them, one of them is still slightly incorrect.
 
russell lloyd
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes okay I reversed the > sign it should say < I think that will help let me check and report back and btw the error message says that i'm trying to convert nulls to strings so let me try this and see if it works better
 
russell lloyd
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay after the changing of the < sign I am now recieving no error messages but the program isn't doing what I want it to do. it isn't creating the objects the way it is supposed to. These are the relevant parts of the code.


This is the relevant code from the card class that I wrote before.


after running the test class I have written for my deck class which only creates a deck object and then prints out the deck object as a string is as follows


Please enter a Suit Between 0-4 and a Value between 1-13 in integer form
Please enter a Suit Between 0-4 and a Value between 1-13 in integer form
Please enter a Suit Between 0-4 and a Value between 1-13 in integer form
Please enter a Suit Between 0-4 and a Value between 1-13 in integer form
0
Queen of Spades/n


i have been working for awhile and i really can't seem to change the output in any way
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain exactly what this line does and why.

Other random notes. On line 1 above there is no need to create a random card if you're going to throw it away on the very next line. You can initialize this to null or leave it uninitialized -- remove "= new Card()" -- and the compiler will ensure that you initialize it before using it.

In the Card constructor taking a suit and value, only the value is enforced to be valid. The else block only applies to the if block immediately before it. If you pass in an invalid suit, it's ignored in favor of suite 0, whatever that happens to be. Here's a common pattern I use:The nice thing about IAE is that your code doesn't have to deal with it since it's unchecked, and that's good because it's a programmer error and your code likely cannot deal with it.
 
russell lloyd
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the point of my toString method in my Deck Class is to take all 52 cards that were "supposedly" created by the constructor and print them out in string formats. So the program is supposed to create an array of card objects where the suit is assigned value 1 - 13 and the suit is assigned value 0 - 3. I don't know if any of this helps but there it is. Anyways in the card class I wrote the methods that convert the numeric values into string representations. The Deck toString() method is supposed to convert all the indices of yourDeck[] into strings concatenate them together and then print out the results. I know you guys are supposed to teach and not just give me answers But as you can see I think my only problems are in the syntax i'm using not the theory. Please try to guide me to the right program because I have worked pretty hard on this and I really don't want to get a bad grade on upcoming tests and such just because I"m not getting some array syntax correct
 
russell lloyd
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that my primary problem is in the joining together of the strings that I have converted the cards into that is what I'm not getting
 
russell lloyd
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay well my program is due in an hour so unless someone can help me i'm pretty screwed. So if anyone could please help me out with the to string method i'm having trouble with that would be the greatest thing ever
thank you
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by russell lloyd:
The Deck toString() method is supposed to convert all the indices of yourDeck[] into strings concatenate them together and then print out the results.

This was the only part that was incorrect (the bolded line in the code I reposted). The line was getting the String form of a card, adding a newline, and assigning this new String to fullDeck, overwriting whatever fullDeck pointed to at the time. All that was needed was to append it to fullDeck.The += operator means "add/append and assign." For example "x += 2" adds 2 to x and is equivalent to "x = x + 2".

I hope you got it before it was due.
 
russell lloyd
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for all the help guys saved the day for sure
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic