• Post Reply Bookmark Topic Watch Topic
  • New Topic

I want to use an add screen to add infromation to a hashmap, but cant get the add button to work

 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working with a hashmap and have passed the table from the menu (where the haspmap is read from a datafile) to the Add screen (I can enter information into the hashmap frpom the consol) but whenever I try to read from the screen itself using an add button I cannot get the button to map to the hashmap?

map.put(RegNo,new Vehicle(RegNo,make,model,cc,colour,month,year,cost));

Will work within the class but not when its within the button
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David, Welcome to CodeRanch!

Unfortunately, there isn't a lot of information in your post about the problem. Since we don't have your code we can't possibly know what is going on. So if you could TellTheDetails it would be easier for us to help.

What would be helpful would be:
1) Code. Show the related code
- make it real code (copy and pasted directly from your program),
- and enough of it to see the issue.
- Try to keep unrelated code out though (otherwise the post gets too big and hard to read)
- And please UseCodeTags
2) Error Messages.
- What happens when 'it doesn't work'?
- What messages do you get?
- What happens that you don't expect to happen?
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More Details
I am using public class Test to create the GUI's and open a menu screen this has the main method.
the public class menu extends JFrame implements ActionListener
Menu creates the menu stucture and opens the appropriate screens.



//reads the file and enters the values into the map

this code works and creates the hashmap which is passed to the next add screen using

the add screen gets the map using the method


My problem is that if I use the console at this point to add a new recod to the map using


the hashmap is accepted
if I try to do this via the screen then the hash map is not updated?
can anyone help me
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button rather than writing tags by hand. Since you are new here, I have correct the tags.

You have shown us how you add Cars via the command line or terminal, but haven't shown us any GUI code. Since your non-GUI code seems correct, I shall move this thread to where we usually discuss GUIs.

By the way: What delimiter are you using for your Scanner? Is it the default (=whitespace)? In which case, the last car I drove had AB60 CDE (or something with similar counts of letters and numbers) as its registration number; would your Scanner read that as one token or two?
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help so far
I am not sure what GUI code to show, the add button is simply


the action listner is


I am currently the default (=whitespace) but am aware of the problem with the registration I have used other tokens such as (,) with file input and output in the past but am unsure of how to change the syntax in this case (I have been cheating and removed the spaces until I get everthing else working), advise on this also would be appriciated.
I am not sure that I am using the code button correctly
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can click the code button and then start writing after the first ] and before the second [. Note there is no space there, so you have to be accurate with the mouse or cursor.
Alternatively, you can write the code (copy-and-paste) is better, highlight the whole code block and click the code button to add tags after writing.
Note code tags maintain indentation and multiple spaces, and don't wrap, so avoid very long lines. Use spaces, not tab for indentation. You can get text editors which will automatically change a tab to spaces; most people prefer tab→4 spaces.

For delimiters you can try separating the tokens with commas in the input file, and use \w*,\w* as the delimiter. Look in the java.util.regex.Pattern class or the Java™ Tutorials that \w means whitespace and comma isn't a "metacharacter". If you express that as a String you would have to double up the \ characters "\\w*,\\w*". You can try putting "" round all your tokens and splitting on quote mark-whitespace*-quote mark instead, but I would prefer commas. You can find out what the * means in the Tutorials.

If you are using text fields, you won't have that problem; you can put AB60 CDE in your registration number field and not have any problems about the space in the middle.
I have corrected the code tags, and shall delete my half post in a little while.

Where are you getting the variables from? You quote RegNo, model, etc, but are they local variables, parameters or fields? (By the way: that should be regNo, and it would be better to call the field regNoTextField than txtRegNo).
I don't like the addActionListener(this) concept. I would prefer to have classes associated with the buttons. It would be too much like hard work to change that just as the moment, however. Get your adding working first. Change the adding block to read like this:See what prints out when you click the button. Are you suffering any exceptions? If that doesn't give any explanation, we shall have to see some more code, I think.
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the map in the above line in the button has a red squiggly which says map cannot be resolved
If I run the programme with your extra bit of code (ignoring the warnings) I get.

null null null 0 null null 0 0.00Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:

It looks to me as if the map.put (when placed within the button) is not recognised, outside the button in the method it is?
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to go back to the Vehicle#toString() method and see how that printout corresponds to it. It is almost as if you were passing no information to the Vehicle constructor, and were getting the default values for all its fields. It would appear you are definitely putting a Vehicle in the map; otherwise how could you get it back?

If you are getting compiler errors, make sure to sort them out first. Have you spelt "map" wrongly somewhere?
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is also possible you have put a Vehicle into the map earlier with all its fields left at default values. Does Vehicle have a no-arguments constructor? If so, try removing it and compiling Vehicle again and running the app again.
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did leave an earlier default open I have now closed it. I am now getting the following error message.

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
map cannot be resolved
map cannot be resolved

at AddVehicle.actionPerformed(AddVehicle.java:286)

which is the


in the add button
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, where did you declare "map"? Is it a field or a local variable or a parameter? Did you spell it the same in both places? Have you confused map and Map, which to Java™ are different?
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been importing map as aparameter

It appears to work within the body of this method (when I add it through the console), but when I place the map in the add button the map has the red squiggely.

When I put public HashMap<String, Vehicle> map; in the place it is in below the red squiggly goes away



but when I use the add button after filling in the various fields I get
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AddVehicle.actionPerformed(AddVehicle.java:253)
The usual place (note the line number has changed as I have modified some code)
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Importing isn't the correct term.
Don't give any of your fields public access (unless they are public static final).
You appear to be passing it as a constructor parameter. Change that to read (Map<String, Vehicle> map), then you can pass other sorts of Map. Similarly change the type of the field from HashMap to Map<String, Vehicle>.
Why are you passing the Map from outside your class?

Suggest: go back to your non-GUI classes. Create a public addCarDetailsToMap(String, String, String, int, String, String, int, double) method and pass those tokens from your Scanner as arguments to it, rather than adding everything in the loop. It is awkward having a method with 8 parameters, but we'll have to take the rough with the smooth. Get that working; you can move the contents of that loop into that method.
By the way: you don't need String.valueOf(String) to get a String!
Pass a reference of an object of that class to the AddVehicle GUI class constructor, as a field. Now you have something with a public method you can call, with a reference inside your GUI class. Now try passing the contents of your eight text fields (or parsed contents) to that method, and see whether that works any better.
You may need a method which prints the contents of the Map; if you keep my //test line in the adding method, you will see immediately what you have added.

Why have you got a static Scanner for System.in inside a GUI class? Would you actually use it there?
Which is line 253 that you are getting the Exception from?
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was to create a vechicle class, get some details from a datafile and put it into a hashmap and create a menu system which has screens to display the contents of the map (display all, display by make) and a screen to add additional cars and a delete screen, also create some reports based on make and model.
I have a test class (which contains the main method), to create a menu class, I am using the menu to open and create the other classes (screens) and also read the datafile and create the map. I am then passing this map to the other classes (add and Display Classes), the display class is working well it is iterating through the map and appending the result to a text area. That is why I have a static Scanner for System.in inside a GUI class?

I already have aVehicle class constructor, is this what you mean by a AddVehicle GUI class constructor?



PS. Thanks for all the time you have taken.
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Signing off for tonight
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of the no-arguments constructor in Vehicle. It is doing no good, and we have seen it is potentially harmful.
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Stockman wrote:I already have aVehicle class constructor, is this what you mean by a AddVehicle GUI class constructor?
No. Not at all.

You already have a class which maintains a Map<String, Vehicle>. From what you are telling me, that is working nicely. So use it again. Enhance it with an addCarDetailsToMap() method, as I said earlier. Instead of adding a car from that loop you showed at first, use that loop to gather all the necessary information from the data file, and call that method. Then you can see whether that method is working. Once you have got it working, create an instance of the class which includes the map, and pass it to the GUI class constructor. If you maintain a reference to that as a field of the GUI class, or maybe better still of a class which has the GUI as a field, you can use the actionPerformed method to gain access to that object and to invoke the addCarDetailsEtc method.
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid long lines in code; they don't wrap, and are difficult to read. Newline after each ; please. It costs nothing to spread the code out, and your dense lines in the constructors are difficult to read. One space before and after each binary operator, including = please.
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you had any luck with it?
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have had a few tries today but didn't quite get what you ment.
I think I get it now if I create a class on its own which creates the hashmap then the other classes within the package should be able to see the hash map if I use the class name and the map name then I should have access to the map.
Will have a go at this latter today. Will let you know how I got on, again thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have understood, but you don't want to be passing ClassName.MapName. You want to be passing objectName.addToMap(), or more precisely, passing objectName, then calling addXXXToMap(...) on it.
 
David Stockman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got it working !!!
I had a false impression that you had to write and read from file to the map by iterating through the map, didn't quite get that you pass it as a single object. Looking back I can see that was what you were telling me but I was so far down the wrong way of looking at it I couldn't get it. Thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done It's a great moment when the penny drops, isn't it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!