Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 java forums?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
giuseppe105




PostPosted: Thu Oct 22, 2009 4:10 pm   Post subject: java forums?

Why cant i post a question on them?

Ill ask my question here and hope i get help or it moves.

I am using java and i'm using the paint method to paint graphics.
g.drawImage(image object,int x,int y,object)

but since i am making a 2d grid i need to load an array of images. in this image array there will be values of the images that will appear in order.

i have code that looks like this rite now
for (int i = 0; i < fileLength; i++)
{
g.drawImage(texture[i],i,i,null);
}

this code is compileing fine but returning at least a hundred exceptions after i run the application.

what am i doing wrong or how can i do this in a different way?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Oct 22, 2009 6:15 pm   Post subject: RE:java forums?

You really need to include the exception it's throwing, preferably along with its error message.
giuseppe105




PostPosted: Thu Oct 22, 2009 6:48 pm   Post subject: Re: RE:java forums?

DemonWasp @ Thu Oct 22, 2009 6:15 pm wrote:
You really need to include the exception it's throwing, preferably along with its error message.


the program compiles with no errors but when it runs it returns all this.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at drawMap.paint(drawMap.java:43)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:277)
at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
at javax.swing.JComponent.paint(JComponent.java:1013)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at drawMap.paint(drawMap.java:43)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:277)
at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
at javax.swing.JComponent.paint(JComponent.java:1013)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Process completed.
TheGuardian001




PostPosted: Thu Oct 22, 2009 6:59 pm   Post subject: Re: java forums?

Yeah, those would be errors.

Specifically it's a null pointer exception, meaning you probably called a variable that wasn't initialized.
You'll want to look at the lines mentioned (drawMap.java line 43) and check what variables it call upon, then insure you've initialized them.

And for future reference, at the top of the main Java forum are three sub forums titled "Java Tutorials," "Java Submissions," and "Java Help." You'll want to look at those.
giuseppe105




PostPosted: Thu Oct 22, 2009 8:36 pm   Post subject: Re: java forums?

this is my code

import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;

public class drawMap extends JPanel
{
BufferedImage grass;
mapLoader ml;
BufferedImage[] textures;

int mapLength;

public drawMap()
{
mapLoader ml = new mapLoader();
mapLength = ml.getLength();
try
{
ml.loadMap();
grass = ImageIO.read(new File("Grass.png"));
} catch (IOException e)
{
}

BufferedImage[] textures = new BufferedImage[mapLength];

Map<String, BufferedImage> textureList = new HashMap<String, BufferedImage>();
textureList.put("grass",grass);

for (int x = 0; x <mapLength; x++)
{
textures[x] = textureList.get(ml.texture[x]);
}
}

public void paint(Graphics g)
{
for (int i = 0; i < 4; i++)
{
g.drawImage(textures[i],i*32,0,null);
}
}
}

i think i know what your talking about i give all the values to the textures array in the constructor
but how would i give it the values so that the paint method can see them there?
TheGuardian001




PostPosted: Thu Oct 22, 2009 8:54 pm   Post subject: Re: java forums?

Firstly, you've given code we can't run, which is never a good idea. If you have extra classes (like mapLoader) that are required to run your program, we'll need those classes if you want us to be able to help debug the code.

Anyway...

The paint method can see the values fine, the problem is that you're attempting to use a value that doesn't exist.

are you sure that
code:

textures[x] = textureList.get(ml.texture[x]);

is initializing at least 4 values? If any value stored in ml.texture[3] to ml.texture[0] has not been initialized, it will give textures[x] that null value and throw an error.
giuseppe105




PostPosted: Thu Oct 22, 2009 9:04 pm   Post subject: Re: java forums?

I forgot to mention that when i put mapLength in the paint method for loop, instead of the number 4. the program runs properly with no errors returned. the only problem was that it never drew the graphics. i just got a Grey screen. so i assumed that there was something wrong with the program.

what im understanding is that the program is perfectly fine but that doesn't explain why i get a Grey screen.
giuseppe105




PostPosted: Tue Oct 27, 2009 3:08 pm   Post subject: Re: java forums?

Since this question is unanswered i though i would tell you what was wrong.

the way i was getting the file length in my drawMap class was returning a 0 so when i told the program to loop from 0 to (maplength) it would just skip the loop because mapLength was 0. I replaced the idea behind the drawing and now it works with 3 layers of graphics being displayed.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: