very new to java string array question

Discussion in 'Java' started by jason, Dec 18, 2009.

  1. jason

    jason Guest

    hello, below is my code. the error message is:
    Exception in thread "main" java.lang.NullPointerException
    at WebsiteReader.main(WebsiteReader.java:23)

    ----program below--
    import java.io.*;
    import java.net.URL;

    public class WebsiteReader
    {
    public static BufferedReader read(String url) throws Exception{
    return new BufferedReader(
    new InputStreamReader(
    new URL(url).openStream()));
    }

    public static void main (String[] args) throws Exception{
    BufferedReader reader = read("http://www.google.com");
    String line = reader.readLine();
    int Counter=0;
    String MyStrArray[]= null;


    while (line != null) {
    System.out.println(line);
    System.out.println(String.valueOf(line));
    line = reader.readLine();
    MyStrArray[Counter]= line;

    Counter=Counter+1;
    }
    System.out.println(Counter);
    }
    }
    jason, Dec 18, 2009
    #1
    1. Advertising

  2. jason

    Eric Sosman Guest

    On 12/18/2009 10:52 AM, jason wrote:
    > hello, below is my code. the error message is:
    > Exception in thread "main" java.lang.NullPointerException
    > at WebsiteReader.main(WebsiteReader.java:23)
    >
    > ----program below--
    > import java.io.*;
    > import java.net.URL;
    >
    > public class WebsiteReader
    > {
    > public static BufferedReader read(String url) throws Exception{
    > return new BufferedReader(
    > new InputStreamReader(
    > new URL(url).openStream()));
    > }
    >
    > public static void main (String[] args) throws Exception{
    > BufferedReader reader = read("http://www.google.com");
    > String line = reader.readLine();
    > int Counter=0;
    > String MyStrArray[]= null;


    Here you declare MyStrArray (poor choice of name, but
    that's another topic) as a variable that can refer to an
    array of references to String objects. You also set the
    variable to null, meaning that it does not refer to anything
    at all right now: There is no array of String anywhere, just
    a variable that could refer to such a thing if you were to
    create one.

    >
    >
    > while (line != null) {
    > System.out.println(line);
    > System.out.println(String.valueOf(line));
    > line = reader.readLine();
    > MyStrArray[Counter]= line;


    And here you try to store a String reference in the array
    that MyStrArray refers to. Unfortunately, MyStrArray is null
    and does not refer to any array. Boom!

    > Counter=Counter+1;
    > }
    > System.out.println(Counter);
    > }
    > }


    I'm not sure how to advise you to change your program,
    because the real problem appears to be that you have not yet
    grasped some rather fundamental notions about Java, including

    - Arrays are objects, and like all objects must be created
    before being used.

    - An array, once created, has a fixed size: You cannot just
    add or remove array elements. You can change the values
    of those elements, but the elements themselves are always
    there until the array itself is destroyed.

    - The variables your program manipulates are primitives (int,
    double, and so on) or are references that can point to
    object instances (arrays, Strings, BufferedReaders, ...).

    - A reference variable whose value is null points to nothing
    at all. You cannot use that variable to refer to an object
    until you first create an object and make the variable point
    to it.

    I have the feeling that until you've grokked these (and other)
    basic bits of Java knowledge, correcting your program will just
    give you a chunk of code that you won't understand and won't know
    how to modify further. Back to the textbook; eventually, this
    will all make more sense than it does now.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 18, 2009
    #2
    1. Advertising

  3. jason

    Lew Guest

    Eric Sosman wrote:
    >      I have the feeling that until you've grokked these (and other)
    > basic bits of Java knowledge, correcting your program will just
    > give you a chunk of code that you won't understand and won't know
    > how to modify further.  Back to the textbook; eventually, this
    > will all make more sense than it does now.
    >


    Additionally:
    <http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html>
    <http://java.sun.com/developer/onlineTraining/index.jsp>

    --
    Lew
    Lew, Dec 18, 2009
    #3
  4. jason

    jason Guest

    On Dec 18, 11:10 am, Eric Sosman <> wrote:
    > On 12/18/2009 10:52 AM, jason wrote:
    >
    >
    >
    > > hello, below is my code. the error message is:
    > > Exception in thread "main" java.lang.NullPointerException
    > >    at WebsiteReader.main(WebsiteReader.java:23)

    >
    > > ----program below--
    > > import java.io.*;
    > > import java.net.URL;

    >
    > > public class WebsiteReader
    > > {
    > >    public static BufferedReader read(String url) throws Exception{
    > >            return new BufferedReader(
    > >                    new InputStreamReader(
    > >                            new URL(url).openStream()));
    > >            }

    >
    > >    public static void main (String[] args) throws Exception{
    > >            BufferedReader reader = read("http://www.google.com");
    > >            String line = reader.readLine();
    > >            int Counter=0;
    > >            String MyStrArray[]= null;

    >
    >      Here you declare MyStrArray (poor choice of name, but
    > that's another topic) as a variable that can refer to an
    > array of references to String objects.  You also set the
    > variable to null, meaning that it does not refer to anything
    > at all right now: There is no array of String anywhere, just
    > a variable that could refer to such a thing if you were to
    > create one.
    >
    >
    >
    > >            while (line != null) {
    > >                    System.out.println(line);
    > >                    System.out.println(String.valueOf(line));
    > >                    line = reader.readLine();
    > >                    MyStrArray[Counter]= line;

    >
    >      And here you try to store a String reference in the array
    > that MyStrArray refers to.  Unfortunately, MyStrArray is null
    > and does not refer to any array.  Boom!
    >
    > >                    Counter=Counter+1;
    > >            }
    > >    System.out.println(Counter);
    > >    }
    > > }

    >
    >      I'm not sure how to advise you to change your program,
    > because the real problem appears to be that you have not yet
    > grasped some rather fundamental notions about Java, including
    >
    >      - Arrays are objects, and like all objects must be created
    >        before being used.
    >
    >      - An array, once created, has a fixed size: You cannot just
    >        add or remove array elements.  You can change the values
    >        of those elements, but the elements themselves are always
    >        there until the array itself is destroyed.
    >
    >      - The variables your program manipulates are primitives (int,
    >        double, and so on) or are references that can point to
    >        object instances (arrays, Strings, BufferedReaders, ...).
    >
    >      - A reference variable whose value is null points to nothing
    >        at all.  You cannot use that variable to refer to an object
    >        until you first create an object and make the variable point
    >        to it.
    >
    >      I have the feeling that until you've grokked these (and other)
    > basic bits of Java knowledge, correcting your program will just
    > give you a chunk of code that you won't understand and won't know
    > how to modify further.  Back to the textbook; eventually, this
    > will all make more sense than it does now.
    >
    > --
    > Eric Sosman
    >


    i fixed it. thank you for taking the time to praise your own knowledge
    of java.
    jason, Dec 18, 2009
    #4
  5. jason

    Lew Guest

    jason wrote:
    > i [sic] fixed it. thank you for taking the time to praise your own knowledge
    > of java [sic].


    Did you mean that to be as snide as it sounded?

    --
    Lew
    Lew, Dec 18, 2009
    #5
  6. jason

    Guest

    If you plan to crawl the Web from
    Java you'd better fake your user agent, otherwise
    you're in for quite some surprises :)

    URL url = new URL("http://www.google.com");
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-Agent", "Mozilla/4.1... bla bla
    bla"

    Just search for a very common user agent and use that
    one. Been there, done that: it shall works much better
    faking the user agent.

    Oh, don't pay too much attention to the snobbish
    attitude of people in this group, c.l.j.p. is
    full of people full of themselves ;)

    They have in common that they're both JLS *and* grammar
    nazis.

    It's crazy but you can find forums where you have discussion
    where several Extactiq people do participate and where people
    will gently take the time to answer your questions without
    feeling like they're there to show off their knowledge.

    The second most important thing you'll find in here is that
    people consider every Gosling brain fart to be the holy gospel.

    I've had both IBM JVM engineers and Apple JVM engineers answer
    me and participate in discussions with me in a much more friendly
    and instructive manner than what you'll get here.

    Here you'll get "RTFM / RTFJLS" type answers accompanied with
    grammar-naziness (as exemplified by Lew's very constructive
    participation in this thread where he quotes your lowercase 'i'
    to follow it by a [sic]).

    Rant off, fake your user agent :)
    , Dec 18, 2009
    #6
  7. jason

    Lew Guest

    wrote:
    > Oh, don't pay too much attention to the snobbish
    > attitude of people in this group, c.l.j.p. is
    > full of people full of themselves ;)
    >


    That's the pot calling the kettle black. Vas-t'en, cochon.

    > They have in common that they're both JLS *and* grammar
    > nazis.
    >


    Godwin's Law.
    <http://en.wikipedia.org/wiki/Godwin%27s_Law>

    > It's crazy but you can find forums where you have discussion
    > where several Extactiq people do participate and where people
    > will gently take the time to answer your questions without
    > feeling like they're there to show off their knowledge.
    >


    It's even crazier when someone like Eric Sosman provides a correct,
    helpful answer to what looks like a sincere question, then someone
    like you comes along and starts spouting off unfounded hostile
    nonsense as if he'd done something wrong.

    > The second most important thing you'll find in here is that
    > people consider every Gosling brain fart to be the holy gospel.
    >


    What the ...? What are you on about? There's been no mention of
    Gosling other than your comment here, and certainly no dogma in this
    thread other than your trollish crud.

    > I've had both IBM JVM engineers and Apple JVM engineers answer
    > me and participate in discussions with me in a much more friendly
    > and instructive manner than what you'll get here.
    >


    You aren't setting any kind of friendly or instructive example, unlike
    Eric Sosman, who gave a very friendly and instructive answer to the
    OP's question.

    > Here you'll get "RTFM / RTFJLS" type answers accompanied with
    > grammar-naziness (as exemplified by Lew's very constructive
    > participation in this thread where he quotes your lowercase 'i'
    > to follow it by a [sic]).
    >


    Hush, child, the grownups are talking. Fermes ta bec.

    --
    Lew
    Lew, Dec 18, 2009
    #7
  8. jason wrote:
    > On Dec 18, 11:10 am, Eric Sosman <> wrote:
    >> On 12/18/2009 10:52 AM, jason wrote:
    >>
    >>
    >>
    >>> hello, below is my code. the error message is:
    >>> Exception in thread "main" java.lang.NullPointerException
    >>> at WebsiteReader.main(WebsiteReader.java:23)
    >>> ----program below--
    >>> import java.io.*;
    >>> import java.net.URL;
    >>> public class WebsiteReader
    >>> {
    >>> public static BufferedReader read(String url) throws Exception{
    >>> return new BufferedReader(
    >>> new InputStreamReader(
    >>> new URL(url).openStream()));
    >>> }
    >>> public static void main (String[] args) throws Exception{
    >>> BufferedReader reader = read("http://www.google.com");
    >>> String line = reader.readLine();
    >>> int Counter=0;
    >>> String MyStrArray[]= null;

    >> Here you declare MyStrArray (poor choice of name, but
    >> that's another topic) as a variable that can refer to an
    >> array of references to String objects. You also set the
    >> variable to null, meaning that it does not refer to anything
    >> at all right now: There is no array of String anywhere, just
    >> a variable that could refer to such a thing if you were to
    >> create one.
    >>
    >>
    >>
    >>> while (line != null) {
    >>> System.out.println(line);
    >>> System.out.println(String.valueOf(line));
    >>> line = reader.readLine();
    >>> MyStrArray[Counter]= line;

    >> And here you try to store a String reference in the array
    >> that MyStrArray refers to. Unfortunately, MyStrArray is null
    >> and does not refer to any array. Boom!
    >>
    >>> Counter=Counter+1;
    >>> }
    >>> System.out.println(Counter);
    >>> }
    >>> }

    >> I'm not sure how to advise you to change your program,
    >> because the real problem appears to be that you have not yet
    >> grasped some rather fundamental notions about Java, including
    >>
    >> - Arrays are objects, and like all objects must be created
    >> before being used.
    >>
    >> - An array, once created, has a fixed size: You cannot just
    >> add or remove array elements. You can change the values
    >> of those elements, but the elements themselves are always
    >> there until the array itself is destroyed.
    >>
    >> - The variables your program manipulates are primitives (int,
    >> double, and so on) or are references that can point to
    >> object instances (arrays, Strings, BufferedReaders, ...).
    >>
    >> - A reference variable whose value is null points to nothing
    >> at all. You cannot use that variable to refer to an object
    >> until you first create an object and make the variable point
    >> to it.
    >>
    >> I have the feeling that until you've grokked these (and other)
    >> basic bits of Java knowledge, correcting your program will just
    >> give you a chunk of code that you won't understand and won't know
    >> how to modify further. Back to the textbook; eventually, this
    >> will all make more sense than it does now.
    >>
    >> --
    >> Eric Sosman
    >>

    >
    > i fixed it. thank you for taking the time to praise your own knowledge
    > of java.


    No reason to get so snippy. For starters, it's mostly *not* your code -
    the part that works is taken from a DZone snippet
    (http://snippets.dzone.com/posts/show/3553), which you might have had
    the grace to acknowledge. The part that you are having problems with -
    pretty basic Java, BTW, which is why Eric said what he said, and not
    incorrectly - is the minimal stuff you added.

    AHS
    Arved Sandstrom, Dec 19, 2009
    #8
  9. jason

    Roedy Green Guest

    On Fri, 18 Dec 2009 07:52:20 -0800 (PST), jason
    <> wrote, quoted or indirectly quoted someone
    who said :

    >String MyStrArray[]= null;


    you are missing a step in the initialisation. See
    http://mindprod.com/jgloss/array.html

    For general help on NullPointerExceptions see
    http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The future has already happened, it just isn’t evenly distributed.
    ~ William Gibson (born: 1948-03-17 age: 61)
    Roedy Green, Dec 20, 2009
    #9
  10. jason

    Roedy Green Guest

    On Fri, 18 Dec 2009 09:32:34 -0800 (PST), jason
    <> wrote, quoted or indirectly quoted someone
    who said :

    >i fixed it. thank you for taking the time to praise your own knowledge
    >of java.


    See http://mindprod.com/jgloss/newsgroups.html

    You just did something that could ensure you don't get responses in
    future.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The future has already happened, it just isn’t evenly distributed.
    ~ William Gibson (born: 1948-03-17 age: 61)
    Roedy Green, Dec 20, 2009
    #10
  11. jason

    Roedy Green Guest

    On Fri, 18 Dec 2009 13:27:24 -0800 (PST), wrote,
    quoted or indirectly quoted someone who said :

    >URLConnection connection = url.openConnection();
    >connection.setRequestProperty("User-Agent", "Mozilla/4.1... bla bla
    >bla"


    The HTTP class is what I use for Web scraping. It fakes in the latest
    Firefox as the user agent by default.

    See http://mindprod.com/products1.html#HTTP

    See http://mindprod.com/jgloss/http.html
    for the magic User-Agent strings to simulate various browsers.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The future has already happened, it just isn’t evenly distributed.
    ~ William Gibson (born: 1948-03-17 age: 61)
    Roedy Green, Dec 20, 2009
    #11
  12. jason

    code learner

    Joined:
    Dec 24, 2010
    Messages:
    19
    MyStrArray[Counter]= line;
    the above line throws NullPointerException
    because of
    String MyStrArray[]= null;
    code learner, Jan 9, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    471
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,616
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    418
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    512
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,287
Loading...

Share This Page