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?
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?
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
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?
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
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.
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?
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.
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
The usual place (note the line number has changed as I have modified some code)
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?
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?
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.
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.
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.
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.
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.