Error: No suitable constructor found for InputStreamReader
Hello. I'm trying to use some java code I got out of a text book, but I get an error message when I try to compile.
The code:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class WebReader extends JFrame {
JTextArea box = new JTextArea("Getting data....");
public WebReader() {
super("Get File Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 300);
JScrollPane pane = new JScrollPane(box);
add(pane);
setVisible(true);
}
void getData(String address) throws MalformedURLException {
setTitle(address);
URL page = new URL(address);
StringBuffer text = new StringBuffer();
try {
HttpURLConnection conn = (HttpURLConnection) page.openConnection();
conn.connect();
InputStreamReader in = new InputStreamReader( (InputStream) conn.getContent() );
BufferedReader buff = new BufferedReader(in);
box.setText("Getting data....");
String line;
do {
line = buff.readLine();
text.append(line + "\n");
} while (line != null);
box.setText(text.toString());
} catch (IOException ioe) {
System.out.println("IO Error: " + ioe.getMessage());
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java WebReader url");
System.exit(1);
}
try {
WebReader app = new WebReader();
app.getData(args[0]);
} catch (MalformedURLException mue) {
System.out.println("Bad URL: " + args[0]);
}
}
}
And the compiler error message:
WebReader.java:26: error: no suitable constructor found for InputStreamReader(InputStream)
InputStreamReader in = new InputStreamReader( (InputStream) conn.getContent() );
constructor InputStreamReader.InputStreamReader(java.io.InputS tream,String) is not applicable
(actual and formal arguments lists differ in length)
constructor InputStreamReader.InputStreamReader(java.io.InputS tream) is not applicable
(actual argument InputStream connot be converted to java.io.InputStream by method invocation conversion)
The error message seems to be tellng me I have used the wrong constructor or perhaps used the constructor incorrectly.
Can anyone tell me what I need to do to make this code compile? Thank you in advance.
The error message continues
Re: Error: No suitable constructor found for InputStreamReader
Well, it appears that conn.getContent() does not return an InputStream (although I would have thought you would have received a class cast exception somewhere if that was the case). I am not familiar with HttpURLConnection but perhaps you want conn.getInputStream().
Jim
Re: Error: No suitable constructor found for InputStreamReader
Quote:
Originally Posted by
jim829
Well, it appears that conn.getContent() does not return an InputStream (although I would have thought you would have received a class cast exception somewhere if that was the case).
CCE is a RuntimeException. The problem is a compile time error... so the code can't have been executed.
It looks likely that there is a class or interface (badly) named InputStream in the same package (or in the same default package).
db
Re: Error: No suitable constructor found for InputStreamReader
Hello.
From db "It looks likely that there is a class or interface (badly) named InputStream in the same package (or in the same default package)."
You are correct that the java.io pckage includes a class InputStream, and a class InputStreamReader.
The class InputStreamReader has the following constructors:
InputStreamReader(InputStream)
InputStreamReader(InputStream, Charset)
InputStreamReader(InputStream, CharsetDecoder)
InputStreamReader(InputStream, String)
I confess I do not know enough to use this info to fix my compiling error.
Re: Error: No suitable constructor found for InputStreamReader
Re: Error: No suitable constructor found for InputStreamReader
Quote:
Originally Posted by
Humphrey Bogart
Hello.
From db "It looks likely that there is a class or interface (badly) named InputStream in the same package (or in the same default package)."
You are correct that the java.io pckage includes a class InputStream, and a class InputStreamReader.
The class InputStreamReader has the following constructors:
InputStreamReader(InputStream)
InputStreamReader(InputStream, Charset)
InputStreamReader(InputStream, CharsetDecoder)
InputStreamReader(InputStream, String)
I confess I do not know enough to use this info to fix my compiling error.
I'm talking about your own code, not the JDK classes. You appear to have a class named InputStream, and InputStreamReader (InputStream) requires a parameter of type java.io.InputStream -- not your own class (or interface) naked InputStream.
Look around for files like InputStream.java / InputStream.class and rename or get rid of them.
db