Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default 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:
    Java 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

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Error: No suitable constructor found for InputStreamReader

    Quote Originally Posted by jim829 View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default 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.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Error: No suitable constructor found for InputStreamReader

    Of course! Duh...
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Error: No suitable constructor found for InputStreamReader

    Quote Originally Posted by Humphrey Bogart View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 03-13-2012, 01:51 PM
  2. MySQL: No suitable driver found.
    By Onra in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 12-15-2011, 02:41 PM
  3. No suitable driver found
    By chinna in forum New To Java
    Replies: 3
    Last Post: 11-30-2011, 01:17 PM
  4. No suitable driver found exception error
    By Crazy Caveman in forum Networking
    Replies: 1
    Last Post: 09-01-2010, 12:03 AM
  5. No suitable driver found
    By geekchick in forum New To Java
    Replies: 2
    Last Post: 08-25-2010, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •