• Post Reply Bookmark Topic Watch Topic
  • New Topic

Jbutton won't work  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a properties file with key values in it, now when I use my search box to search for a key in the properties file to check whether or not it is there - I can't get it to check and return the key value if it is there, could anyone point me in the right direction?
I've tried a few methods but keep getting a null pointer exception when it hits line 15 in the code all the time so I'm stumped at the minute.

My text field is called textFieldSearch

This is my button code




This is my method code
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might close your other thread because I think you are asking the same question twice. Or you can get all your answers in one thread.

Anyway: wherever you think you are getting Exceptions, you aren't. You cannot get that exception from line 15. You must have changed the line numbers.

There is something peculiar about your code. Why do you want to know whether the writing in a text box is the same as the property? Why are you passing the text as a key and then getting the value from your properties object? That is hardly going to be the same ever. Start by explaining what you are trying to do, nice and simply, so even I can understand it. Chances are, that will clear your thinking so you will have a better idea what to do.
You are mixing System.out and JButtons. Swing and command line outputs should be like oil and water: don't try to mix them. Start by looking at your FindSelectedKey method. What does that do? It doesn't seem to use the property anywhere, so it simply disappears into cyber‑limbo never to be seen again. And what are you passing as an argument? If you are passing anything it would be a Boolean because you have got the == operator where I am sure you don't really want it.

Suggest:
  • 1: Change that method so it returns the String or whatever the value of the Property is.
  • 2: Print your properties object with all its pairs.
  • 3: Take keyboard input so you can pass a key and print out the corresponding values.
  • 4: Consider what you want to be equal to what.
  • What you have managed all that lot, you will probably be ready to put the GUI back together. You will also know whether any of your buttons, text fields, properties, etc., are null. Beware: a properties object will often return null if there is the tiniest spelling error in the key.
     
    Alice Hampton
    Ranch Hand
    Posts: 217
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, I wasn't sure if I had put it in the wrong thread to begin with.

    If I remove everything I have put in my button and forget about that method I have.

    I want it so a user comes along and enters a GIT command into the textfield - they then click the 'search' button.

    When the search button is clicked I need to check the properties file I have that contains the GIT commands to see if there is any matching keys to what the user entered.

    If there is a matching key then output the key itself with the value from the properties file and display it in a panel.

    If there is not a matching key in the properties file then output an error message.

    If there is no matching key - retrieve all of the keys that start with the same first letter to that of what the user entered and display that into a panel instead.

    I hope by breaking it down I could make it clear - I am new to java and once I fully understand how to do this then I am hoping i'll know for the future - thanks for your time.
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So pass some GIT texts to the properties object and print the property values returned.
    You are not checking for matching keys. You are checking whether the object returned is the same as the key: as I said earlier, hardly likely to be so. You can check whether the returned property is null or not; that will probably tell you whether you have a “real” key.
    Do you not have containsKey or containsValue methods? Can you get the keys out as Sets or even sorted sets? Then you can us methods like contains. There is probably a nice elegant way to do that sort of thing by getting a Stream from your Set, but I don't know how to do it.

    I still think you need to sort out the basics of what you want to do before you try coding it. Back to the Java Tutorials (look for sets and sorted sets). Have a look at the properties object, whether it has an entrySet method, which you get a set from.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!