Java void cannot be dereferenced

«void cannot be dereferenced» error..

Возможно, адреса электронной почты являются анонимными для этой группы или вам требуется разрешение на просмотр адресов электронной почты ее участников, чтобы увидеть исходное сообщение.

I have made many frames for my project but my problem is that when I
close one of them.. the whole program closes

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ConfigGui extends JPanel implements ActionListener private JButton okButtonConfig;
private JButton cancelButtonConfig;
private JTextField inputConfig;

public ConfigGui() okButtonConfig = new JButton («Ok»);
cancelButtonConfig = new JButton («Cancel»);
inputConfig = new JTextField (5);

Читайте также:  Sum strings as numbers codewars python

setPreferredSize (new Dimension (229, 84));
setLayout (null);

add (okButtonConfig);
add (cancelButtonConfig);
add (inputConfig);

okButtonConfig.setBounds (35, 55, 50, 20);
cancelButtonConfig.setBounds (95, 55, 95, 20);
inputConfig.setBounds (35, 10, 155, 25);

public void actionPerformed(ActionEvent jnd)
if (jnd.getSource()==cancelButtonConfig)
configGui().exit(0);
>
>

public void configGui()
JFrame frame = new JFrame («Set the Dictionary»);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new ConfigGui());
frame.pack();
frame.setVisible (true);
>
>

my problem here is in the if statement.. it says «void cannot be
dereferenced»..
I made the frame in void method because I am calling it in another
class..
Any tips please? 🙁

Peter Duniho

Возможно, адреса электронной почты являются анонимными для этой группы или вам требуется разрешение на просмотр адресов электронной почты ее участников, чтобы увидеть исходное сообщение.

> Hi everyone, I really suck at this..

Suck at what? Java? Programming in general?

Don’t worry. Assuming you’re just starting out, sucking is to be
expected. That’s just how learning new things goes. A person will
_always_ suck at it to start out. Ironically, it’s the people who usually
learn new things quickly that get the most discouraged by sucking at
things when they start out, because they’re so unaccustomed to the
feeling. But rest assured, it’s perfectly normal.

> I have made many frames for my project but my problem is that when I
> close one of them.. the whole program closes

See my other reply regarding that particular issue. This is related to
the fact that you’re calling System.exit() rather than just closing the
specific window.

As for the code you posted.

> public void actionPerformed(ActionEvent jnd)
> > if (jnd.getSource()==cancelButtonConfig)
> > configGui().exit(0);
> >
> >
>
> public void configGui()
> > JFrame frame = new JFrame («Set the Dictionary»);
> frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
> frame.getContentPane().add (new ConfigGui());
> frame.pack();
> frame.setVisible (true);
> >
> >
>
> my problem here is in the if statement.. it says «void cannot be
> dereferenced»..
> I made the frame in void method because I am calling it in another
> class..
> Any tips please? 🙁

Well, first tip: «void» is specifically the absence of a return value, so
trying to dereference «void» (by calling the «exit()» method on the
non-existent return value) makes no sense at all. Second tip: even if you
could dereference it, you’d need to use a type that implements an «exit()»
method.

In other words, you could fix the «void» part by returning the JFrame
instance you made in the method, but since there’s no «exit()» method on
the JFrame type, you’d still get a compiler error. More broadly, that
combination of methods really doesn’t seem to make any sense. You seem to
be trying to terminate your application by creating not one but _two_ new
Swing components (a JFrame and a JPanel subclass to put inside it).

Finally, making the «configGui()» method an instance method also seems
odd. You essentially have a situation in which to create a new JFrame
that has a ConfigGui instance in it, you have to already have a ConfigGui
instance. That seems a bit self-referential and not in a way that is
likely to be a good design, even if it’s really what you intended.

Источник

How can I solve Void cannot be dereferenced?

We are asked to create a calculator but before I do that I must correct the codes given in order for me to know what I must add in my calculator but I do not know how to solve the «void cannot be referenced » error. how can I do that? here’s the code that I made. the error is line 80 and 87

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Button extends JFrame implements ActionListener < private static int width=600; private static int height=300; private JLabel[] labelJL = new JLabel[3]; private JTextField[] textJT = new JTextField[3]; private JButton[] AddClearExit = new JButton[3]; public Button() < Container pane = getContentPane(); setTitle("USING BUTTON EVENT"); setSize(width,height); setLayout(null); labelJL[0] = new JLabel("FIRST NUMBER",0); labelJL[0].setLocation(150,20); labelJL[0].setSize(100,20); pane.add(labelJL[0]); textJT[0] = new JTextField(); textJT[0].setLocation(260,20); textJT[0].setSize(200,20); pane.add(textJT[0]); labelJL[1] = new JLabel("SECOND NUMBER",0); labelJL[1].setLocation(150,50); labelJL[1].setSize(100,20); pane.add(labelJL[1]); textJT[1] = new JTextField(); textJT[1].setLocation(260,50); textJT[1].setSize(200,20); pane.add(textJT[1]); labelJL[2] = new JLabel("Total Sum",0); labelJL[2].setLocation(150,80); labelJL[2].setSize(100,20); pane.add(labelJL[2]); textJT[2] = new JTextField(); textJT[2].setLocation(260,80); textJT[2].setSize(200,20); textJT[2].setEditable(false); pane.add(textJT[2]); AddClearExit[0] = new JButton("Sum"); AddClearExit[0].setLocation(50,150); AddClearExit[0].setSize(150,20); AddClearExit[0].addActionListener(this); pane.add(AddClearExit[0]); AddClearExit[1] = new JButton("Clear"); AddClearExit[1].setLocation(210,150); AddClearExit[1]. setSize(150,20); AddClearExit[1].addActionListener(this); pane.add(AddClearExit[1]); AddClearExit[2] = new JButton("Exit"); AddClearExit[2].setLocation(370,150); AddClearExit[2].setSize(150,20); AddClearExit[2].addActionListener(this); pane.add(AddClearExit[2]); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); >public void actionPerformed(ActionEvent ae) < String temp = ""; long fn=0, sn=0; try < fn=Long.parseLong(textJT[0].getText()); sn=Long.parseLong(textJT[0].getText()); >catch (Exception ne) < for(int x=0;x<3;textJT[x].setText(temp).x++); >if(ae.getActionCommand().equals("Sum")) temp+=sum(fn,sn); if(ae.getActionCommand().equals("Clear")) for(int x=0;x <3;textJT[x].setText(temp).x++); if(ae.getActionCommand().equals("Exit")) dispose(); textJT[2].setText(temp); >public long sum(long x,long y) < return x+y; >public static void main (String args[]) < new Button(); >>
  • 2 Contributors
  • 1 Reply
  • 1K Views
  • 7 Hours Discussion Span
  • Latest Post 11 Years Ago Latest Post by JamesCherrill

We’re a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Источник

void cannot be dereferenced problem

send pies

posted 5 years ago

  • Report post to moderator
  • I’m creating a Session API and i’m trying to set some member vars. Code:

    I have a setter method which sets them. Code:

    But when I come to compile it I get the following error:

    Help much appreciated. Many thanks!

    Marshal

    Python

    send pies

    posted 5 years ago

  • Report post to moderator
  • What’s the return type of this?

    «void» right? Which is Java’s way of saying «nothing». So what do you expect to be able to do with it? Also nothing.

    What are you expecting .toString() to be called on here?

    Tim Driven Development | Test until the fear goes away

    send pies

    posted 5 years ago

  • Report post to moderator
  • Tim Cooke wrote: What’s the return type of this?

    «void» right? Which is Java’s way of saying «nothing». So what do you expect to be able to do with it? Also nothing.

    What are you expecting .toString() to be called on here?

    Any need for the funny attitude? It doesn’t matter ive sorted it now.

    Источник

    Why void pointer cannot be dereferenced?

    A void pointer cannot be dereferenced. . This is because a void pointer has no data type associated with it. There is no way the compiler can know what type of data is pointed to by the void pointer. The solution is typecasting.

    Can a pointer of type void be dereferenced?

    1) void pointers cannot be dereferenced.

    When can a void pointer be dereferenced?

    When does the void pointer can be dereferenced? Explanation: By casting the pointer to another data type, it can be dereferenced from the void pointer.

    Can void pointer be deleted?

    Void pointer is a pointer which is not associate with any data types. . It is not safe to delete a void pointer in C/C++ because delete needs to call the destructor of whatever object it’s destroying, and it is impossible to do that if it doesn’t know the type.

    Can a void pointer be null?

    A void pointer is a pointer that can point to any type of object, but does not know what type of object it points to. . A void pointer can be a null pointer.

    Error: Cannot Be Dereferenced (Java Tutorial)

    44 related questions found

    What is NULL and void pointer?

    Null pointer is specially reserved value of a pointer. Void pointer is a specific pointer type. . Null pointer is used for assigning 0 to a pointer variable of any type. Void pointer is used for storing address of other variable irrespective of its datatype.

    Which pointer does not exist?

    NULL Pointer is a pointer which is pointing to nothing. In case, if we don’t have address to be assigned to a pointer, then we can simply use NULL.

    What is void pointer?

    A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. . Some Interesting Facts: 1) void pointers cannot be dereferenced. For example the following program doesn’t compile.

    What is delete function C++?

    When delete is used to deallocate memory for a C++ class object, the object’s destructor is called before the object’s memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.

    What we Cannot do on a void pointer?

    Because the void pointer is used to cast the variables only, So pointer arithmetic can’t be done in a void pointer.

    What is generic pointer?

    When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. Hence the term Generic pointer. .

    Does return type void?

    ______________ have the return type void. Explanation: Constructor creates an Object and Destructor destroys the object. They are not supposed to return anything, not even void. . Explanation: void fundamental type is used in the cases of a and c.

    Can you dereference void?

    It can be a double , an unsigned long , or even an array of char s. However, it is possible to get dereference a void * through casting, because the cast operator contains the information about object type.

    Why reference is not same as a pointer?

    Why reference is not same as a pointer? A reference can never be null. A reference once established cannot be changed. Reference doesn’t need an explicit dereferencing mechanism.

    How do you declare an integer pointer to a pointer?

    1. Pointer declarations use the * operator. .
    2. In the example above, p is a pointer, and its type will be specifically be referred to as «pointer to int», because it stores the address of an integer variable. .
    3. The type is important.

    What does deleting a pointer do C++?

    Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression.

    1. Delete can be used by either using Delete operator or Delete [ ] operator.
    2. New operator is used for dynamic memory allocation which puts variables on heap memory.

    How do I get a free pointer?

    5 Answers. Calling free() on a pointer doesn’t change it, only marks memory as free. Your pointer will still point to the same location which will contain the same value, but that value can now get overwritten at any time, so you should never use a pointer after it is freed.

    Do you need to delete pointers C++?

    Just because something is a pointer does not mean you should call delete . A pointer is simply a variable that contains a memory address. What is pointed to should only be deleted if it was created with new. very old code or C code being worked into c++ may have functions that expect the user to delete the data.

    Is it better to use malloc () or calloc ()?

    Note: It would be better to use malloc over calloc, unless we want the zero-initialization because malloc is faster than calloc. So if we just want to copy some stuff or do something that doesn’t require filling of the blocks with zeros, then malloc would be a better choice.

    Why is the void pointer useful?

    Why we use void pointers? We use void pointers because of its reusability. Void pointers can store the object of any type, and we can retrieve the object of any type by using the indirection operator with proper typecasting.

    What is size of void pointer?

    The size of void pointer varies system to system. If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes.

    How do you fix a dangling pointer?

    The dangling pointer errors can be avoided by initializing the pointer to the NULL value. If we assign the NULL value to the pointer, then the pointer will not point to the de-allocated memory. Assigning NULL value to the pointer means that the pointer is not pointing to any memory location.

    Why are dangling pointers bad?

    A dangling pointer is a pointer to memory that your program should not use. Using dangling pointers is a common mistake among beginners. . One kind of dangling pointer is an address in a part of memory that the OS knows does not belong to your process. Using such a dangling pointer leads to a memory fault.

    How can I call malloc?

    1. Call C++ «new» operator, like «int *p=new int[10];». .
    2. Call the C function «malloc», which takes a byte count and returns a pointer, like «int *p=(int *)malloc(40);». .
    3. Allocate space on the stack (see the next lecture).

    Is null and void?

    Canceled, invalid, as in The lease is now null and void. This phrase is actually redundant, since null means “void,” that is, “ineffective.” It was first recorded in 1669.

    Источник

    Оцените статью