import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.sql.*;

class MyCanvas extends Canvas
{
Image img;
public MyCanvas(Image img)
{
 this.img = img;
}
public void paint (Graphics g)
{
 if (img != null)
{
g.drawImage(img, 0, 0, 1000, 1000, this);
}
}
public void setImage (Image img)
{
this.img = img;
}
}


public class ImagesTest implements ActionListener, WindowListener 
{
Frame fr = new Frame ("CBIRS");
Label Label1 = new Label("   ");
Button Button1 = new Button("OPEN IMAGE");
Button Button2 = new Button("FIND IMAGE");
Image Image1,img1,img2;
MyCanvas Canvas1;
public String d;
public FileDialog fd = new FileDialog(fr,"Open", FileDialog.LOAD);
int iw1, ih1;
int iw2, ih2;
int pixels1[];
int pixels2[];
public double[] hist1 = new double[256];
public double[] hist2 = new double[256];
public double[] dist = new double[5];
public int y;
double distance = 0;
double distance1 = 0;
int j=0;

void initialize ()
{ 
fr.setSize(1000,1000);
fr.setLocation(300,300);
fr.setBackground(Color.black);
fr.setLayout(new FlowLayout());
fr.add(Label1);
fr.add(Button1);
fr.add(Button2);
fr.addWindowListener(this);
Button1.addActionListener(this);
Button2.addActionListener(this);
Canvas1 = new MyCanvas(null);
Canvas1.setSize(1000,1000);
fr.add(Canvas1);
fr.setTitle("CBIRS");
fr.show();
}

public void imageload () 
{
fd.show();
if(fd.getFile() == null)
{ 
Label1.setText("You have not chosen any image files yet");
}
else
{
d = (fd.getDirectory() + fd.getFile());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image1 = toolkit.getImage(d);
Canvas1.setImage(Image1);
Canvas1.repaint();
} 
}

public void QueryHist()
{
try 
{
//getting pixels
d = (fd.getDirectory() + fd.getFile());
Toolkit toolkit = Toolkit.getDefaultToolkit();
img1 = toolkit.getImage(d);
iw1 = img1.getWidth(null);
ih1 = img1.getHeight(null);
pixels1 = new int[iw1*ih1];
PixelGrabber pg  = new PixelGrabber(img1,0,0,iw1,ih1,pixels1,0,iw1);
pg.grabPixels();

//calculating hist array
for (int i=0; i<iw1*ih1; i++) 
{
int p1 = pixels1[i];
int r1 = 0xff & (p1 >> 16);
int g1 = 0xff & (p1 >> 8);
int b1 = 0xff & (p1);
y = (int) (.33 * r1 + .56 * g1+ .11 * b1);
hist1[y]++;
}
}
catch (InterruptedException e)
{
System.out.println("interrupted");
return;
}
}


public void RefHist()
{
try
{
for(int j=0;j<5;j++)
{

int i=0;
String url="jdbc:odbc:ImageData";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(url); 
PreparedStatement psmnt1 = connection.prepareStatement("select Array from ImageOLE where 

ID="+(j+1234)+" ");
ResultSet rs = psmnt1.executeQuery();
if(rs.next())
{
InputStream fis1;
FileOutputStream fos;
fis1 = rs.getBinaryStream("Array");
DataInputStream in = new DataInputStream(fis1);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

String c=" ";
String a[]=new String[256];
while ((c = br.readLine()) != null)
{
a[i]=c;
i++;
}
for(i=0;i<256;i++)
{
hist2[i]=Double.parseDouble(a[i]);
}
fis1.close();
}
rs.close();
psmnt1.close();
connection.close();
}

Eudis();
}
catch(SQLException e)
{
System.out.println("SQL error");
return;
}
catch(IOException e)
{
System.out.println("IOException");
return;
}
catch(ClassNotFoundException e)
{
System.out.println("class not found");
return;
}
}


//calculating Eu distance
public void Eudis()
{
int j=0;
try
{
QueryHist();
for(int i=0;i<256;i++)
{
distance = distance + Math.pow((hist1[i] - hist2[i]), 2);
}
distance1= Math.sqrt(distance);
dist[j]=distance1;


//putting distance into database
String url="jdbc:odbc:ImageData";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection(url); 
Statement smnt = c.createStatement();
int row=smnt.executeUpdate("update ImageOLE set Distance="+dist[j]+"  where ID="+(j+1)+" ");
j=j+1;
c.commit();
}
catch(SQLException e)
{
System.out.println("SQL error");
return;
}
catch(ClassNotFoundException e)
{
System.out.println("class not found");
return;
}
}

 
public void windowClosing(WindowEvent e)
{
System.exit(0); 
}
public void windowActivated(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowOpened(WindowEvent e)
{
}


public void actionPerformed(ActionEvent event)
{
Button b = (Button)event.getSource();
if(b == Button1) 
{
imageload();
}
if(b == Button2)
{
RefHist();
} 
}

public static void main(String args[])
{
ImagesTest a = new ImagesTest(); 
a.initialize();
} 
}

this my code and Exception is


H:\java>jdk1.6.0_13\bin\javac ImagesTest.java
Note: ImagesTest.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

H:\java>jdk1.6.0_13\bin\java ImagesTest
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:99
1)
at java.lang.Double.parseDouble(Double.java:510)
at ImagesTest.RefHist(ImagesTest.java:149)
at ImagesTest.actionPerformed(ImagesTest.java:246)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.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)

plz tell me where's the problem?

The error says your problem is at line 149 in ImagesTest.java:
at ImagesTest.RefHist(ImagesTest.java:149)

But I just ran your code and did not get the error. Did you try running it again? Do you always get this same error?

The error says your problem is at line 149 in ImagesTest.java:
at ImagesTest.RefHist(ImagesTest.java:149)

But I just ran your code and did not get the error. Did you try running it again? Do you always get this same error?

yes i ran it again, it works well for Button-open image but when i press Button -find image it shows this Exception.
yes i know error is in line 149 but i m not getting it.

It would help if the code you posted corresponded to the error messages. Line 149 is for(i=0;i<256;i++) - no NPE there!
After that you loop thru all 256 elements of the array, but you previously populated only as many elements as there lines in the data file, so if there are <256 lines in the file yo will have un-populated elements in the array after that, hence NPE when you try to refernce them?

It would help if the code you posted corresponded to the error messages. Line 149 is for(i=0;i<256;i++) - no NPE there!
After that you loop thru all 256 elements of the array, but you previously populated only as many elements as there lines in the data file, so if there are <256 lines in the file yo will have un-populated elements in the array after that, hence NPE when you try to refernce them?

actually i have written an array of 256 values in a file and stored it in database.
now i am reading those 256 values from the file so i m sure that 256 values are being read and there are no unpopulated array elements

Edited 6 Years Ago by Nidhi S.: n/a

So which line (according to the code you posted earlier) does the NPE point to?

that is what i m asking.
it is possible that line 149 may not be the line where error occured, but where it first surfaced.this type of error normally occurs when we try to call a method with null object.
i thought RefHist() is the method where the problem is so i made a seperate programe of RefHist() and took the output on console and its working fine .
so i am not getting where the problem is.

When Java displays the Exception it tells you the number of the line where the Exception was created, as in
"at ImagesTest.RefHist(ImagesTest.java:149)"
This is essential information for debugging.

However, it's useless without a line-numbered program listing of the exact code that caused the NPE. The code you posted is not the exact code that you were running when you got the NPE - it has a line 149, but that is not the same line 149 that the NPE message refers to - so its unhelpful.

Please post the relevant code showing the exact line that is referred to in the Java error message.

When Java displays the Exception it tells you the number of the line where the Exception was created, as in
"at ImagesTest.RefHist(ImagesTest.java:149)"
This is essential information for debugging.

However, it's useless without a line-numbered program listing of the exact code that caused the NPE. The code you posted is not the exact code that you were running when you got the NPE - it has a line 149, but that is not the same line 149 that the NPE message refers to - so its unhelpful.

Please post the relevant code showing the exact line that is referred to in the Java error message.

yes
line 149 in original code is actually line 151 in the above posted code.
so the exception is at line
hist2=Double.parseDouble(a);
do you want me to post code outside

?
if the code is working fine seperately so i was thinking to use it seperately for application as my project deadline is approaching and i need to finish it.

The only way I can see for that to generate an NPE is for a to be un-initialised.
Insert a System.out.println(i + " " + a); immediately before that line and let's see exactly what's happening.

The only way I can see for that to generate an NPE is for a to be un-initialised.
Insert a System.out.println(i + " " + a); immediately before that line and let's see exactly what's happening.

H:\java>jdk1.6.0_13\bin\java ImagesTest
0 394.0
1 308.0
2 314.0
3 978.0
4 1064.0
5 2032.0
6 2278.0
7 493.0
8 460.0
9 684.0
10 638.0
11 974.0
12 811.0
13 930.0
14 653.0
15 708.0
16 614.0
17 632.0
18 647.0
19 601.0
20 630.0
21 616.0
22 593.0
23 593.0
24 604.0
25 584.0
26 573.0
27 650.0
28 624.0
29 605.0
30 646.0
31 609.0
32 614.0
33 639.0
34 620.0
35 626.0
36 635.0
37 611.0
38 594.0
39 526.0
40 594.0
41 614.0
42 585.0
43 594.0
44 642.0
45 612.0
46 595.0
47 641.0
48 636.0
49 714.0
50 724.0
51 804.0
52 944.0
53 1029.0
54 1001.0
55 1033.0
56 1056.0
57 1169.0
58 1658.0
59 1181.0
60 1938.0
61 1215.0
62 2268.0
63 3666.0
64 3006.0
65 2179.0
66 2050.0
67 2041.0
68 1442.0
69 1998.0
70 2352.0
71 1169.0
72 1238.0
73 918.0
74 831.0
75 745.0
76 775.0
77 732.0
78 658.0
79 771.0
80 827.0
81 1032.0
82 873.0
83 1219.0
84 1592.0
85 1664.0
86 1873.0
87 1115.0
88 1617.0
89 781.0
90 659.0
91 673.0
92 676.0
93 686.0
94 703.0
95 735.0
96 738.0
97 799.0
98 897.0
99 887.0
100 865.0
101 922.0
102 1020.0
103 1055.0
104 1262.0
105 1416.0
106 1235.0
107 1533.0
108 1352.0
109 1748.0
110 1395.0
111 1922.0
112 2380.0
113 2093.0
114 2049.0
115 1837.0
116 2253.0
117 1673.0
118 1873.0
119 1874.0
120 1520.0
121 1712.0
122 1525.0
123 1629.0
124 1602.0
125 1913.0
126 2079.0
127 1505.0
128 1585.0
129 1444.0
130 1424.0
131 1226.0
132 1357.0
133 1271.0
134 1063.0
135 1120.0
136 1058.0
137 1097.0
138 1082.0
139 1110.0
140 1076.0
141 968.0
142 1186.0
143 805.0
144 794.0
145 815.0
146 710.0
147 838.0
148 712.0
149 713.0
150 678.0
151 710.0
152 688.0
153 726.0
154 649.0
155 716.0
156 670.0
157 681.0
158 652.0
159 652.0
160 643.0
161 590.0
162 597.0
163 558.0
164 561.0
165 536.0
166 473.0
167 449.0
168 418.0
169 368.0
170 398.0
171 375.0
172 337.0
173 335.0
174 349.0
175 354.0
176 345.0
177 340.0
178 339.0
179 286.0
180 300.0
181 252.0
182 212.0
183 209.0
184 185.0
185 173.0
186 121.0
187 86.0
188 67.0
189 68.0
190 40.0
191 98.0
192 26.0
193 19.0
194 40.0
195 37.0
196 27.0
197 21.0
198 30.0
199 35.0
200 34.0
201 24.0
202 34.0
203 31.0
204 19.0
205 27.0
206 16.0
207 18.0
208 13.0
209 8.0
210 0.0
211 0.0
212 0.0
213 0.0
214 0.0
215 0.0
216 0.0
217 0.0
218 0.0
219 0.0
220 0.0
221 0.0
222 0.0
223 0.0
224 0.0
225 0.0
226 0.0
227 0.0
228 0.0
229 0.0
230 0.0
231 0.0
232 0.0
233 0.0
234 0.0
235 0.0
236 0.0
237 0.0
238 0.0
239 0.0
240 0.0
241 0.0
242 0.0
243 0.0
244 0.0
245 0.0
246 0.0
247 0.0
248 0.0
249 0.0
250 0.0
251 0.0
252 0.0
253 0.0
254 0.0
255 0.0
0 null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:99
1)
at java.lang.Double.parseDouble(Double.java:510)
at ImagesTest.RefHist(ImagesTest.java:150)
at ImagesTest.actionPerformed(ImagesTest.java:249)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.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)
this is the output.
notice that the last value before exception is 0 null and that is the cause of NPE.
in the seperate prog that i made i read data from file in database and written it in another txt file .that file does not have this 0 null.

Looks like the loop is being executed twice, and has no data on the second execution.

how can i check that the loop is being executed twice?
plz suggest some solution.

The 0,null print after 255,0.0 seems pretty definitive to me.
Use more print statements.
Your code wasn't indented, so its too hard to see where the loops begin and end, but line 123 has a j=0 to 4 loop which appears to surround the relevant area, and there's something funny about where you close the result set, but I don't know what that's all about.

The 0,null print after 255,0.0 seems pretty definitive to me.
Use more print statements.
Your code wasn't indented, so its too hard to see where the loops begin and end, but line 123 has a j=0 to 4 loop which appears to surround the relevant area, and there's something funny about where you close the result set, but I don't know what that's all about.

i've solved it.
problem: that stings are always terminated by a null pointer.so as i was reading string from a file and converting it to double it was throwing a null pointer exception due to the null termination.
solution: i read that null termination as an array element(257th) and did type conversion as
while(a != null)
{
hist2=Double.parseDouble(a);
}
thanks for the help.

This question has already been answered. Start a new discussion instead.