Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Need some Java Cocoa app help

Need some Java Cocoa app help
Thread Tools
Junior Member
Join Date: Oct 2000
Location: Londonderry, VT USA
Status: Offline
Reply With Quote
Dec 10, 2000, 12:13 AM
 
Any Java experts out there? I'm trying to get the Java Tutorial (Developer>Documentation>Cocoa&g t;JavaTutorial)to run correctly. I compile and link OK, but am getting a null pointer when the "tempImage.tempChanged()" method is called from the following class:
----------------
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;

public class TempController {

NSTextField celsius;

NSTextField fahrenheit;

TempImageView tempImage;

public void convert(NSTextField sender) {


if (sender ==celsius){
int f =(int)((9.0/5.0 *celsius.intValue())+32);
fahrenheit.setIntValue(f);
}
else
if (sender ==fahrenheit){
int c =(int)((fahrenheit.intValue()-32)*5.0/9.0);
celsius.setIntValue(c);
}

tempImage.tempChanged(fahrenheit.intValue());
}
}
--------------
The method is defined in the TempImageView class:
----------------
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;

public class TempImageView extends NSImageView {
protected NSImage coldImage;
protected NSImage moderateImage;
protected NSImage hotImage;

public TempImageView (NSRect frame){
//load images
super(frame);
String h =NSBundle.mainBundle().pathForResource ("Cold","tiff");
if (h !=null){
coldImage =new NSImage(h,true);
}
else
System.err.println ("Image Cold.tiff not found.");
h =NSBundle.mainBundle().pathForResource ("Moderate","tiff");
if (h !=null){
moderateImage =new NSImage(h,true);
}
else
System.err.println ("Image Moderate.tiff not found.");
h =NSBundle.mainBundle().pathForResource ("Hot","tiff");
if (h !=null){
hotImage =new NSImage(h,true);
}
else
System.err.println ("Image Hot.tiff not found.");
setEditable (false);
setImage (moderateImage);
setImageAlignment (NSImageCell.ImageAlignCenter);
setImageFrameStyle (NSImageCell.ImageFrameNone);
setImageScaling (NSImageCell.ScaleProportionally);
}
public void tempChanged (int degree){
if (degree < 45){
setImage (coldImage);
}
else
if (degree > 75){
setImage (hotImage);
}
else
setImage (moderateImage);
}
}
----------------
The images load correctly and the the default (moderateImage) shows correctly in the app window. Any one know why the call to "tempChanged" is a null pointer?
thanks,
--dick peskin
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Dec 10, 2000, 03:46 PM
 
Assuming this is the line where you're getting the error:

tempImage.tempChanged(fahrenheit.intValue());

Is the outlet "tempImage" attached properly to an instance of TempImageView? If not, it's value would be NULL, explaining the error you're getting.

Ali

     
rpeskin  (op)
Junior Member
Join Date: Oct 2000
Location: Londonderry, VT USA
Status: Offline
Reply With Quote
Dec 10, 2000, 04:40 PM
 
Originally posted by ali:
Assuming this is the line where you're getting the error:

tempImage.tempChanged(fahrenheit.intValue());

Is the outlet "tempImage" attached properly to an instance of TempImageView?
I appears to be correctly connected. I get the default "tempImage" (image) appearing as a result of the TempImageView constructor.

If not, it's value would be NULL, explaining the error you're getting.

The error occurs as runtime and the Java console (in PB) lists an unhandled NUL POINTER exception associated with the call
tempImage.tempChanged(fahrenheit.intValue()); from the TempController. If I put a local text call in place it works fine, and if I use a text definition (e.g. System.out.println) in place of the tempChanged method in TempImageView I get the NULL POINTER. It appears to me that the class TempController doesn't know about the class TempViewImage,
--dick


     
Dedicated MacNNer
Join Date: Jul 1999
Status: Offline
Reply With Quote
Dec 10, 2000, 09:25 PM
 
It's your lucky day

I had the same problem(I think).

The reason the error message is being sent is because the source images did not ship with the java tutorial, and therefore aren't being found/loaded. You can make you own or use these i made up:
http://home.earthlink.net/~notepad/Cold.tiff
http://home.earthlink.net/~notepad/Moderate.tiff
http://home.earthlink.net/~notepad/Hot.tiff

Save them to disk or in your project-folder.
Import into project inside project builder.(make local copy?sure, why not?)
Compile.

Should work now.

Look V.00000000000000000000000000001 of my cocoa/java web browser. Hah right...
Maybe one day I'll actually learn something.


PS. Ooooooops only saw that last part of your message now.... so probably not the missing images.

[This message has been edited by havannas (edited 12-10-2000).]
     
rpeskin  (op)
Junior Member
Join Date: Oct 2000
Location: Londonderry, VT USA
Status: Offline
Reply With Quote
Dec 10, 2000, 11:14 PM
 
Originally posted by havannas:
It's your lucky day

I had the same problem(I think).

The reason the error message is being sent is because the source images did not ship with the java tutorial, and therefore aren't being found/loaded. You can make you own or use these i made up:
http://home.earthlink.net/~notepad/Cold.tiff
http://home.earthlink.net/~notepad/Moderate.tiff
http://home.earthlink.net/~notepad/Hot.tiff

Save them to disk or in your project-folder.
Import into project inside project builder.(make local copy?sure, why not?)
Compile.

Should work now.

Look V.00000000000000000000000000001 of my cocoa/java web browser. Hah right...
Maybe one day I'll actually learn something.


PS. Ooooooops only saw that last part of your message now.... so probably not the missing images.

[This message has been edited by havannas (edited 12-10-2000).]
-------
I actually created y own tiff imaes and they do load correctly, so it is not the images. Did you ever get this tutorial to work correctly? (If so, can I see you class code?)The problem seems to be that the TempController class knows nothing about the TempImageView class. This apears to be some sort of Java issue, or I have a corrupted PB.
--dick

     
Dedicated MacNNer
Join Date: Jul 1999
Status: Offline
Reply With Quote
Dec 11, 2000, 08:24 AM
 
I think Ali may be correct, maybe you didn't link everything in interface builder. Since that part goes relatively quickly, you could back up your current files(or start anew) and redo the interface builder section. Than re-export your code to project builder and cut and paste your your old code over the new code, since what you really want is a new .nib file. Just remember to back up your current code first. For what its worth heres my code(I didn't see much difference.... slighty different else if usage...slighty different formatting):

-------------------------------------------------------------

/* TempImageView */

import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;


public class TempImageView extends NSImageView
{


protected NSImage coldImage;
protected NSImage moderateImage;
protected NSImage hotImage;

///---------------------------------------------------- constructor method

public TempImageView(NSRect frame)
{
//load images

super(frame);

String h = NSBundle.mainBundle().pathForResource ("Cold", "tiff");
if (h != null)
{
coldImage = new NSImage(h, true);
}
else
{
System.err.println ("Image Cold.tiff not found.");
}


h = NSBundle.mainBundle().pathForResource ("Moderate", "tiff");
if (h != null)
{
moderateImage = new NSImage(h, true);
}
else
{
System.err.println ("Image Moderate.tiff not found.");
}


h = NSBundle.mainBundle().pathForResource ("Hot", "tiff");
if (h != null)
{
hotImage = new NSImage(h, true);
}
else
{
System.err.println ("Image Hot.tiff not found.");
}


setEditable (false);
setImage (moderateImage);
setImageAlignment (NSImageCell.ImageAlignCenter);
setImageFrameStyle (NSImageCell.ImageFrameNone);
setImageScaling (NSImageCell.ScaleProportionally);

}

///---------------------------------------------------- changer method

public void tempDidChange (int degree)
{

if (degree < 45)
{
setImage (coldImage);
}
else if (degree > 75)
{
setImage(hotImage);
}
else
{
setImage(moderateImage);
}

}

///----------------------------------------------------

}
     
Dedicated MacNNer
Join Date: Jul 1999
Status: Offline
Reply With Quote
Dec 11, 2000, 08:38 AM
 
Here's my controller class....

i noticed you changed the line--
"tempImage.tempDidChange (fahrenheit.intValue());"
to
"tempImage.tempChanged(fahrenheit.intValue()); "

Is this the same name you used in interface builder?
I don't know if it's hurting anything, but the lack of spaces after your operators (!=null,=(int)) is really disturbing my delicate psyche.

-------------------------------------------------

/* TempController */

import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;

public class TempController {

NSTextField celsius;
NSTextField fahrenheit;
TempImageView tempImage;

public void convert(NSTextField sender)
{

if (sender == celsius)
{
int f = (int) ((9.0/5.0 * celsius.intValue()) + 32);
fahrenheit.setIntValue(f);
}
else if (sender == fahrenheit)
{
int c = (int) ((fahrenheit.intValue() - 32) * 5.0/9.0);
celsius.setIntValue(c);
}

tempImage.tempDidChange (fahrenheit.intValue());

}

}
     
rpeskin  (op)
Junior Member
Join Date: Oct 2000
Location: Londonderry, VT USA
Status: Offline
Reply With Quote
Dec 11, 2000, 09:17 AM
 
i noticed you changed the line--
"tempImage.tempDidChange (fahrenheit.intValue());"
to
"tempImage.tempChanged(fahrenheit.intValue()); "

Is this the same name you used in interface builder?
I don't know if it's hurting anything, but the lack of spaces after your operators (!=null,=(int)) is really disturbing my delicate psyche.
----------------------
As I recall, tempImage is a name used in Interface Builder, but the method name (tempImage.tempChanged(fahrenheit.intValue()) doesn't get involved. Perhaps I am not using IB correctly.
--dick
     
Dedicated MacNNer
Join Date: Jul 1999
Status: Offline
Reply With Quote
Dec 11, 2000, 12:03 PM
 
i think your right about the method not being used in interface builder, so the name doesn't matter for linking. Hmmm... i dont know, id probably just try to redo the Interface builder section again. I've been playing with these tools since DP3 and only now am getting a grasp of them. I hope they keep the tools free, and flesh out the documentation. This java tutorial is great cause it actually taught me how the thing works. Still a bit sketchy on things though.
     
rpeskin  (op)
Junior Member
Join Date: Oct 2000
Location: Londonderry, VT USA
Status: Offline
Reply With Quote
Dec 11, 2000, 10:10 PM
 
Originally posted by havannas:
i think your right about the method not being used in interface builder, so the name doesn't matter for linking. Hmmm... i dont know, id probably just try to redo the Interface builder section again. I've been playing with these tools since DP3 and only now am getting a grasp of them. I hope they keep the tools free, and flesh out the documentation. This java tutorial is great cause it actually taught me how the thing works. Still a bit sketchy on things though.
-------------
The Java problem is solved. In a sense, Bill was correct; the tempImage instance of TempImageView class was not instantiated. The Interface Builder generated .nib file was corrupted and the "outlet" connection broken. This experience does point up an issue with Interface Builder. In using this tool one relies on IB to set up the "outlet" objects (instance variables) and "actions" (methods) for custom classes. But these are really not instantiated until run time. This makes it very difficult to debug in some situations (like the one I ran into.) It would be nice if there were at least a text file that one could examine to see that all the "connections".
Thanks for everyone's advice.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:20 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2