Double can not be dereferenced?

String mins = minsField.getText(); int Mins; try { Mins = Integer.parseInt(mins); } catch (NumberFormatException e) { Mins = 0; } double hours = Mins / 60; hours.setText(hoursminsfield); 

The problem is that Double cannot be dereferenced .

+7
source share
3 answers

EDIT 4/23/12

double cannot be dereferenced is the error that some Java compilers give when you try to call a method on a primitive. It seems to me that double has no such method would be more useful, but that I know.

From your code, it seems that you think you can copy the text representation of hours to hoursminfield by doing hours.setText (hoursminfield); This has a few errors: 1) hours is a double , which is a primitive type, and there are no methods that you can call. This is what gives you the error you requested. 2) you do not say what type of hoursminfield, maybe you have not announced it yet. 3) it is unusual to set the value of a variable, considering it an argument to the method. This happens sometimes, but not usually.

The lines of code that do what you see fit are as follows:

 String hoursrminfield; // you better declare any variable you are using // wrap hours in a Double, then use toString() on that Double hoursminfield = Double.valueOf(hours).toString(); // or else a different way to wrap in a double using the Double constructor: (new Double(hours)).toString(); // or else use the very helpful valueOf() method from the class String which will // create a string version of any primitive type: hoursminfield = String.valueOf(hours); 

ORIGINAL RESPONSE (addressed another problem in your code):

In double hours = Mins / 60; you separate two int s. You will get the int value of this division, so if Mins = 43; double hours = min / 60; // Mins / 60 is int = 0. Assigning it to a double clock, // the clock doubles to zero.

What you need to do:

 double hours = Mins / ((double) 60); 

or something like that, you need to drop part of your division by double in order to force the division to be done using double , not int s.

+14
source

You did not specify a language, but if it is Java, there is a big difference between the base type double and the class double .

In any case, your setText seems to be wrong. The setText method refers to the data field, not the data you are trying to insert:

 hoursminsfield.setText (hours); 

In other words, you want to set the text of the field using the double you just calculated. If you can go through the double, this is another matter that may need to be studied.

Another thing:

 double hours = Mins / 60; 

will be if Mins is an integer, give you an integer value, which you then put in double. This means that it will be truncated. If you want you to maintain accuracy after separation, you can use something like:

 double hours = (double) Mins / 60.0; 

(although it can only work with one of these changes, I prefer to make all expressions explicit).

+2
source

How about this way

 double hours = Mins / 60.0 

I always use the above operator to get a double value

0
source

All Articles