Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

java - Double cannot 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT 4/23/12

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

From your code, it seems you think you can copy a text representation of hours into hoursminfield by doing hours.setText(hoursminfield); This has a few errors: 1) hours is a double which is a primitive type, there are NO methods you can call on it. This is what gives you the error you asked about. 2) you don't say what type hoursminfield is, maybe you haven't even declared it yet. 3) it is unusual to set the value of a variable by having it be the argument to a method. It happens sometimes, but not usually.

The lines of code that do what you seem to want are:

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 ANSWER (addressed a different problem in your code):

In double hours = Mins / 60; you are dividing two ints. You will get the int value of that division, so if Mins = 43; double hours = Mins / 60; // Mins / 60 is an int = 0. assigning it to double hours makes // hours a double equal to zero.

What you need to do is:

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

or something like that, you need to cast some part of your division to a double in order to force the division to be done with doubles and not ints.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...