• Post Reply Bookmark Topic Watch Topic
  • New Topic

void is an invalid type for the variable actionPerformed

 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am getting this error and just can not figure out what is causing it. I have looked at code from 2 different books. I have google searched.


Here is part of my program that contains the code giving me problems. I posted the program from the beginning to the problem code in case there is something up above it that I don't have coded right yielding the error.




Any help is much appreciated.


Janet











 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your code; always use them. Doesn't it look better
I also moved the line in () (No 141) to a line by itself and commented it out with // Otherwise the lines were too long.

I got a completely different compiler error when I tried your code. There appear to be } missing at the end of the code but even when I added them back I got an illegal start of type error (and lots of other errors). That usually means you have more { or not as many } as you are supposed to and you are using the keyword public when you are still inside a method.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually think that putting an actionPerformed method in a display class is a serious design error. But you haven't put that method into anything which implements ActionListener, so you will have problems with that later on.
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input. I commented out the line and retyped but still get that same error.
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what to do here. I have followed the textbooks.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are getting a void is incorrect type for actionPerformed error?
I copied and pasted your code and got a completely different error myself.
campbell@campbellsComputer:~/java$ javac PayRoll.java
PayRoll.java:139: error: illegal start of expression
public void actionPerformed(ActionEvent e){
^
PayRoll.java:139: error: illegal start of expression
public void actionPerformed(ActionEvent e){
^
PayRoll.java:139: error: ';' expected
public void actionPerformed(ActionEvent e){
^
PayRoll.java:139: error: ';' expected
public void actionPerformed(ActionEvent e){
^
PayRoll.java:146: error: class, interface, or enum expected
}
^
5 errors
Sort out the first error and then see what happens. Illegal start of type usually means too many {s or too few }s before that point.
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You copied and pasted all of my code? I still get the void is an invalid type for the variable actionPerformed error.
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is all of my program. You may be getting errors that I don't get because I did not post all of the code.




 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I copied and pasted all your code and get an illegal start of type error for line 139. You have seen a copy of the error message. You need a } before that.
So I added the } and the illegal start of type error disappeared.
Then I got errors about not finding PayRecord2 or Employee which told me there are classes which you haven't shown us, at which point I stopped.

Why are the two arrays marked static?
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have copied and pasted all the new code and got the same error. You have a new method starting at line 139 which is inside another method. I think you have missed out
something.addActionListener(new ActionListener()
{

before that point, and missed out }); after line 143. I think there is a logic error in lines 140-142, too.

There may be other errors but they aren't coming up on my terminal.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And which book advises you to pass null as a layout manager?
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You put a } right above 139?


btnAddEmployee.setBounds(189, 176, 117, 29);
frame.getContentPane().add(btnAddEmployee);
}

public void actionPerformed(ActionEvent e){
String fullName = textField_2.getText();
String lastName = textField_3.getText();
String eID = textField_1.getText();
} //now I get an error on this one, I have to have a } there
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am taking a program that did work and add the GUI to it using Windowbuilder. And then filling in the code needed.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I now think there is a different error at line 139. Look at my last‑but‑one post.
At least that automatically created code is easier to read than group layout. But your display will go all wrong if you resize it.

You haven't made the mistake of creating your own class/interface called ActionListener, have you?
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error are you getting now?

No, I do not have another class/interface called ActionListener. I posted what I have. I started out with two classes, one was a small program/class that extended JFrame with main() and calls this program/class. This class that I posted was to extend JPanel but I did not like the layout so I changed to the Swing building it with Windowbuilder. Then I could not use the other class. There was a conflict between the two classes. So, I am using just this class that you have.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My compiler error is the same as before: illegal start of type at line 139. If you look at this post however, I now have a different opinion about the likely programming error. Please copy and paste your compiler errors.

Can you wind the code back to whatever it was before you got the conflict? Did it compile all right then?

I still do not know how you get errors about actionPerformed returning void. ActionListener is a 1‑method interface whose single method returns void, so how do you manage to get error messages about returning void? Are you writing actionPerformed without () following it? Are you always writing (ActionEvent xxx) as its parameters? At least you didn't write a listener class with actionPerformed having a different return type.
 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code that runs, at the least the gui works at this point, the functionality has to be added back in.



 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't this what you want? Move your actionPerformed method to the anonymous ActionListener for the button.

 
Janet Heath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That may be what I need to do. I was trying to following some instructions given. I may have been confused.


Thanks.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags (as mentioned in my first post); I have added them because I had to use the edit button to copy and paste your code.

Where does the isOk method come from? Why are you using the != operator? You realise that method does not do what you think it does? Why is that method there in the first place? It looks as though it is part of the initialise method, and you can't have methods inside methods. If you leave it there and get it to compile, you are going to split the initialise method in half. So whatever you do with it, you need to move it somewhere different.

Never use /* ... */ for commenting out code. Always write // at the beginning of each line.

When I tried to compile that code I got two errors about not being able to find pay record or employee classes.

I think the solution is:-
  • 1: Move the isOk method somewhere else. That will make it compile but neither its name nor its functionality is any good. That is a minor point which you can sort out later. You can also simplify that method greatly.
  • 2: The code starting on line 139old/line 152new looks as if it is part of btnAddEmployee.addActionListener(...); If you convert it to such a call, it becomes a statement and can legitimately stay where it is. The current version with new Employee() is better than before but still has no functionality. I think you can correct that yourself quite easily.
  •  
    Campbell Ritchie
    Marshal
    Posts: 52580
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is an interesting error on line 131. If you get the code to run restore line 131 and see what happens (why haven't we got a devil smilie ?)
     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not getting the error now so I must have misinterpreted or just missed some instruction.

    Thank so much for your help!
     
    Campbell Ritchie
    Marshal
    Posts: 52580
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look at line 131 in the version of the code with the /*...*/ around the isOk method. Restore that line, and see if you can work out what will happen … and then try it out. You can then use the smilie.
     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In answer to a previous post you made Campbell. The != means NOT EQUAL. It is used in Java. I just double checked. I have been doing java a few years now but I am new to GUI. The isOK is checking for empty fields. If empty then fail. User will get a message and have to reenter information which be more efficient. Some of the code is an Instructor. Also, I can use isEmpty() to check for empty fields. I may do that instead.
     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    This is more efficient code. I am making sure that none of the fields are empty. If one field is empty then OK is false. If all of the fields have something then return true.
     
    Paul Clapham
    Sheriff
    Posts: 21889
    36
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've added the "code" tags so your code is more readable.

    You say the code is more "efficient"... well, comparing it to your previous version at least it will work. Efficiency doesn't really come into the picture. However like most beginners you've written too much code. First of all: the variables you're checking for emptiness area already strings, so calling their toString() method is unnecessary as that just returns the original string. So:



    Next, your boolean variable in the if-clause is also unnecessary, since you don't use it anywhere else. So:



    And finally whenever you have "if X return true else return false" you can replace that by "return X". It seems to take a while before new Java programmers get used to boolean values and what they mean, but think about it carefully. "If X return true else return false"... What happens when X is true? You return true. And when X is false? You return false. So:


     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't know why you would say there is too much code. There are some imports that I do not need, not being used that I can take out. I need everything else. I agree with you on the toString(). I pulled that from some code looking for an example of using isEmpty. And I don't know what you mean by efficiency doesn't really come into the picture.

    I was using some code that an Instructor gave. I guess the Instructor is inefficient. He is a tenured professor at a University teaching a Master's Level Object Oriented Java programming class. I am helping out a student who came to me for help. I am not done with the coding. There are still blanks to fill in. I am just trying to catch errors and resolve as I code.

    I am not new to programming, a junior level in java and a beginner in GUI. I have not really programmed in a few years and am a little rusty. I need to practice and stay up on it.

    I will fix that if statement.

    Thanks for your help.
     
    Paul Clapham
    Sheriff
    Posts: 21889
    36
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Janet Heath wrote:I don't know why you would say there is too much code.


    Sorry to be unclear. I was just referring to the method in your last post, not to the entire code base which you posted before that.

    And I don't know what you mean by efficiency doesn't really come into the picture...


    I guess the Instructor is inefficient...


    You seem to be using "inefficient" in a different way than most of us would use it. Generally "efficiency" has to do with how fast some code runs, or how much memory it occupies, or something like that. But you didn't seem to be talking about that sort of thing, at least not with the isOK() method which you posted. What you posted there was definitely an improvement on what you had before, because working code is always an improvement on not-working code. It's just that most people wouldn't associate the word "efficiency" with that sort of improvement. But that's really of little importance, because as you say you're working on improvements as you go.
     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok. No problem.
     
    Campbell Ritchie
    Marshal
    Posts: 52580
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Janet Heath wrote: . . . The isOK is checking for empty fields. If empty then fail. . . .
    Have you tried it with empty fields? I haven't. I don't have to. I have been writing code longer than you and I can see what will happen just by looking. I can also see that changing to isEmpty() will work. I can also see that your && however will not work. There is a style problem with that method. You should not write
    if (something) return true; else return false;
    Look at this style guide and look for §10.5.2.
    Your isOk method now becomes
    return nameField.getText() != "" && otherNameField.getText() != "" & idField.getText() != "";
    …only we already know != doesn't work.

    I get the impression that you are writing hundreds of lines of code without compiling. That is a sure‑fire recipe for disaster. You will have no end of difficulty finding your compiler errors. Look what happened. Somehow you got methods inside methods and failed to add listeners in the right place and you had hundreds of lines of code before you found out anything was wrong. You should write one method or ten lines of code (including blank lines, lone } etc) at most. Then you should compile the whole thing and execute the code. Then you find where the errors are quickly, and they will be easier to correct. You should also test the functionality. When you wrote that isOk method you should have pushed the button when one of the fields is empty and seen what happens.
     
    Campbell Ritchie
    Marshal
    Posts: 52580
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see Paul C has already told you how to shorten the isOk method. Once you have it shortened, all you have to do is find a formula which actually works.

    Beware of giving students too much help if the work is to be assessed. If they are deemed not to have done the work themselves, there is a risk of their receiving a mark of 0 and you could get in trouble yourself.

    Challenge for the student. Rewrite the isOk method with a better name. Use the ... ellipsis operator to take any number of parameters to validate that they are not empty Strings, nor Strings containing whitespace only like this: “   ”
    The listener which creates new Employee objects still has no real functionality.
     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I did not write hundreds of lines of code without compiling. A lot of that code is GUI code produced by Windowbuilder. Then I am adding back in code from a program written previously. And I need to do validity checks. There is about 150 lines of code to this program. I could rename the isOK to isValid. I began writing code in College almost 30 years ago. I got my first programming job almost 20 years ago. This is my first program with GUI. How long have you been programming? You don't seem to understand the isOk. I am checking for ! isEmpty Strings. I don't want empty strings. I know how && work. I know logical operators.
     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    != means not equal and it is an operator which is used in Java. I was thinking you could use it on Strings.
     
    Campbell Ritchie
    Marshal
    Posts: 52580
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I didn't mean to be rude. Sorry.

    Did you read the link I posted about using !=? Here it is again. Yes, you can use it. No, it doesn't do what you want. Unless you use intern() that technique will return the wrong answer for you.
    Have you programmed in C#? The == operator does something different in C# from Java® when you apply it to Strings, so the != operator might behave differently when applied to Strings inC#, too. I have seen people experienced in C# fall into that pitfall when they changed to Java®.

    And you have the && operators wrong in the second version of isOk, too, where you used isEmpty(). What you are doing is equivalent to writing something on the lines of
    !(p && q && r)
    That will give the wrong answer unless all three fields are empty. You want to know whether any of the fields is empty.
     
    Janet Heath
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No worries.

    You are right. You can not use == or != on Strings in Java. It is String.EQUALS or !(String.EQUALS). I have programmed in C, C++, VB and Java but not C#. I don't program all the time so I have bad habits sometimes.

    I understand what you are saying about the &&. Only one of the Strings needs to be empty. That is correct. If isEmpty() then will return true else false. I guess I somehow reversed it when trying to match up with the true and false. I need to use the || then that way only one of them has to be empty which is true. I don't need to use the !.
     
    Campbell Ritchie
    Marshal
    Posts: 52580
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would stick with is empty myself in this particular case.

    For equality btween Strings you should use
    s1.equals(s2)
    or
    s1.equalsIgnoreCase(s2)
    If there is the slightest risk of s1 being null, then swap the two round and use
    s2.equals(s1)
    Particularly if one of the operands is a literal, then use
    "Janet".equals(name)
    because there is absolutely no chance of that code throwing a null exception.

    Yes, I thought that you only want to have one vacant text field in order to throw an error.
    The last code of yours I saw includes
    if(!(fullName.toString().isEmpty() && lastName.toString().isEmpty() && eID.toString().isEmpty()))
    Well, let's get rid of the toString() calls because they are pointless; all they get you is the same String you had before, as somebody else has already told you.
    if(!(fullName.toString().isEmpty() && lastName.toString().isEmpty() && eID.toString().isEmpty()))
    if(!(fullName.isEmpty() && lastName.isEmpty() && eID.isEmpty()))

    Now I am going to add some calls: .trim() which will make a String like "   " empty.
    if(!(fullName.trim().isEmpty() && lastName.trim().isEmpty() && eID.trim().isEmpty()))

    But if you do some logic on that expression it will only produce an error if all three fields are vacant and all three Strings are empty. You need a different formula if you want the error when 1 Stri‍ng is empty.
     
    Campbell Ritchie
    Marshal
    Posts: 52580
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have two options: one is to change the && and the other is to multiply the !
    Not
    !(p && q && r)
    but
    !(p || q || r)
    or
    !p && !q && !r
    The () are needed.
     
    Steve Beckle
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you don't rename your actionPerformed(e) method, you're going to go into an infinite loop, as the button's actionPerformed(ActionEvent e) method will end up calling itself. Rename "your" actionPerformed() method.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!