Re: refreshing JEditorPane
My guess is the problem lies outside the code you posted. Where are you calling this code: from a loop? In the event dispatching thread?
Re: refreshing JEditorPane
I call it in a while loop which is in a method as a part of mouse listener class.
Re: refreshing JEditorPane
this is the loop
Code :
File file = new File("result.html");
while(r>0 & r<l & e>0){
fos = new FileOutputStream(file);
dos = new DataOutputStream(fos);
dos.writeBytes(Results.substring(r, e));
java.net.URL Content = file.toURI().toURL();
if (Content != null) {
tArea[i].setPage(Content);
i++;
}
r = Results.indexOf("<a href=",e);
e = Results.indexOf("Cached",r);
}
I am trying to write the content of "result.html" file on myjeditorpane
Re: refreshing JEditorPane
I solved the problem by clearing the stream description property but now I got this exception
Code :
Exception in thread "Thread-14" java.lang.NullPointerException
at javax.swing.JEditorPane$PageLoader.run(Unknown Source)
what is the reason?
Re: refreshing JEditorPane
I have moved this thread to the correct forum -
AWT / Java Swing - Java Programming Forums
Re: refreshing JEditorPane
Hello nasi,
Can you please post the full version of your code? We can attempt to compile it and generate the error then..
Re: refreshing JEditorPane
it is multiple classes, I can't post all of it. the following is the code that generates this thread
Code :
while(r>0 & r<l & e>0){
dos.writeBytes(Results.substring(r, e));
java.net.URL Content = file.toURI().toURL();
tArea[i].setPage(Content);
r = Results.indexOf("<a href=",e);
e = Results.indexOf("Cached",r);
i++;
}
for (int j=0; j<10; j++){
Document doc = tArea[j].getDocument();
doc.putProperty(Document.StreamDescriptionProperty, null);
}
I need to clear the stream description property to be able to reload my jeditorpane but it causes a NullPointerException.
Re: refreshing JEditorPane
This is similar to what I am doing and generates nullPointer thread. more over it doesn't write the Content to my JEditorPane (tArea) properly
Code :
package test;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.Document;
public class test extends JFrame{
/**
*
*/
private String query="university" ;
private String collection = " site:wikipedia.org";
private String searchEngine = "http://www.google.com";
private StringBuilder Results = new StringBuilder();
private JEditorPane[] tArea = new JEditorPane[10];
private File file ;
private FileOutputStream fos;
private DataOutputStream dos;
private JPanel panel = new JPanel();
public test(){
for (int y=0;y<10;y++){
tArea[y]= new JEditorPane();
panel.add(tArea[y]);
}
this.add(panel);
URL url = null;
try {
url = new URL(searchEngine + "/search?hl=en&source=hp&q="+ URLEncoder.encode(query + collection, "UTF-8")+ "&meta=&aq=f&aqi=g10&aql=&oq=&gs_rfai=" );
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
URLConnection Connection = null;
try {
Connection = (URLConnection)url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Connection.setRequestProperty("User-Agent", "Mozilla 5.0");
InputStream inputStream = null;
try {
inputStream = Connection.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int ch=0;
Results.delete(0, Results.length());
try {
while ((ch=inputStream.read()) > 0) {
Results.append((char)ch);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// System.out.println(Results);
int l = Results.lastIndexOf("<a href="/ last occurrence of <a href
int f = Results.indexOf("Search Results");//beginning of search results
int r = Results.indexOf("<a href=",f);//first <a href after f
int e = Results.indexOf("Cached",r);//first similar(end of results of one link) after r
int i=0;
while(r>0 & r<l & e>0){
file = new File("result.html");
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
dos = new DataOutputStream(fos);
try {
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
dos.writeBytes(Results.substring(r, e));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
dos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
java.net.URL Content = null;
try {
Content = file.toURI().toURL();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(Results.substring(r, e));
try {
tArea[i].setPage(Content);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
r = Results.indexOf("<a href=",e);
e = Results.indexOf("Cached",r);
i++;
file.delete();
}
for (int j=0; j<10; j++){
Document doc = tArea[j].getDocument();
doc.putProperty(Document.StreamDescriptionProperty, null);
}
}
public static void main(String[] args) {
JFrame frame= new test();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Re: refreshing JEditorPane