Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: HTML Unit Help

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HTML Unit Help

    Hey guys, So Im trying to make a program using the HTML Unit library that will login to a site with my login info, and then go to another page of the site and post data to that page. My code, is getting null pointer exceptions, as if the form that Im trying to access does not exist. Here is the code to better understand:

        private void HitActionPerformed(java.awt.event.ActionEvent evt) {                                    
            try {
                String IP;
                String Port;
                String Time;
                IP=Host.getText();
                Port=PortField.getText();
                Time=TimeField.getText();
     
     
     
                         final WebClient webClient = new WebClient();
     
    //This part of the code logs in to the site, it works fine as I get no errors on it
                        final HtmlPage page1 = webClient.getPage("http://atomicbooter.org/");
                        webClient.getCookieManager().setCookiesEnabled(true);
     
        final HtmlForm form = page1.getFirstByXPath("//form[@action='set_state.php?action=login']");
     
                        final HtmlSubmitInput button = form.getFirstByXPath("/html/body/div/div[4]/form/table/tbody/tr[3]/td[2]/input");
                        final HtmlTextInput textField = form.getInputByName("username");
                        final HtmlPasswordInput Pass = form.getInputByName("password");
     
     
                        textField.setValueAttribute("Seegee");
                        Pass.setValueAttribute("PASSWORD");
     
                        final HtmlPage page2 = button.click();
     
     
     
     
     
     
                       //This part of the code goes to the next page that I want.
                        final HtmlPage page3 = webClient.getPage("http://atomicbooter.org/hub.php");
     
            final HtmlForm form2 = page3.getFirstByXPath("/html/body/div/div[6]/form");
     
                        final HtmlSubmitInput button2 = form.getFirstByXPath("/html/body/div/div[6]/form/table/tbody/tr[6]/td/input");
                        final HtmlTextInput textField2 = form.getFirstByXPath("//*[@id=host]");
                        final HtmlTextInput tme = form.getFirstByXPath("/html/body/div/div[6]/form/table/tbody/tr[4]/td[2]/input");
                        final HtmlTextInput prt = form.getFirstByXPath("//*[@id=port]");
                        final HtmlTextInput pwr = form.getFirstByXPath("/html/body/div/div[6]/form/table/tbody/tr[5]/td[2]/input");
     
     
     
                        textField.setValueAttribute(IP);
                        tme.setValueAttribute(Time); // THIS IS LINE 194!!!!!!! WHERE I GET NULL POINTER EXCEPTION!
                        prt.setValueAttribute(Port);
                        pwr.setValueAttribute("100");
     
     
                        final HtmlPage page4 = button.click();
            } catch (IOException ex) {
                Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }
    This is the code without the gui, as you dont need the gui, but the gui is in the same class. Running this code returns "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Master.HitActionPerformed(Master.java:194)"


    The object does exist. Here is the html source code of the second page of the site (The page that I get the error on) for you to see:


    HTML Code:
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
    <head>
    <title>Atomic Booter</title>
    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <meta name='robots' content='noindex,nofollow'>
    <link rel='stylesheet' type='text/css' href='css/blue.css' />
    </head>
    <body>
    <div id='container'>
    <div id='logo'>
    <a href='index.php' title='Home'><img src='images/atomic.png' alt='Atomic Booter' /></a>
    </div>
    <div id='login'>
    Welcome, <a href='profile.php?id=31' title='View My Profile'>seegee</a>!<br />
     
    </div>
    <div class='br'></div>
    <div id='navlist'>
    <ul>
    <li><a href='index.php' title='Home'>Home</a></li>
    <li><a href='hub.php' title='Boot' class='active'>Boot</a></li>
    <li><a href='converter.php' title='Host -> IP'>Host -> IP</a></li>
    <li><a href='ip_logger.php' title='IP Logger'>IP Logger</a></li>
    <li><a href='friends.php' title='Friends'>Friends</a></li>
    <li><a href='enemies.php' title='Enemies'>Enemies</a></li>
    <li><a href='attacks.php' title='My Attacks'>My Attacks</a></li>
     
    <li><br /></li>
    <li><a href='profile.php?id=31' title='View Profile'>View Profile</a></li>
    <li><a href='edit_profile.php' title='Edit Profile'>Edit Profile</a></li>
    <li><a href='messages.php' title='Messages'>Messages</a></li>
    <li><a href='statistics.php' title='Statistics'>Statistics</a></li>
    <li><a href='set_state.php?action=logout' title='Logout'>Logout</a></li>
    </ul>
    </div>
    <div class='content'>
    <h3>Statistics</h3>
    <form>
     
    <table cellpadding='5' cellspacing='5' style='text-align: left;'>
    <tr>
    <td style='width: 60%; text-align: right;'>Server Count:</td>
    <td>2</td>
    </tr>
    <tr>
    <td style='text-align: right;'>Allocated Servers:</td>
    <td>2</td>
    </tr>
    <tr>
    <td style='text-align: right;'>Booter Version:</td>
    <td>1.1</td>
     
    </tr>
    </table>
    </form>
     
    </div>
    <div class='content'>
    <h3>DDoS Attack</h3>
    <form name='booter' action='' method='post'>
    <center>
    Use the form below to initiate an attack:</center><br />
    <table cellpadding='5' cellspacing='5' style='text-align: left;'>
    <tr>
    <td style='text-align: right;' valign='middle'>Flood Type</td>
    <td>
     
    UDP And SYN</td>
    </tr>
    <tr>
    <td style='text-align: right;'>Target</td>
    <td width='50%'><input type='text' name='host' id='host' size='30' value='' /></td>
    </tr>
    <tr>
    <td style='text-align: right;'>Port (0 For Random)</td>
    <td width='50%'><input size='5' name='port' type='text' id='port' value='80'></td>    
    </tr>
    <tr>
    <td style='text-align: right;' valign='middle'>Time:</td>
    <td><input type='text' name='time' size='2' value='35' /> seconds</td>
     
    </tr>
    <tr>
    <td style='text-align: right;' valign='middle'>Power:</td>
    <td><input type='text' name='power' size='2' value='50' /> %</td>
    </tr>
    <tr>
    <td colspan='2' valign='right' style='text-align: right;'><input type='submit' name='boot' class='button' value='Flood' /></td>
    </tr>
    </table>
     
    </form>
    </div>
    <div class='br'></div>
    <div id='footer'>
     
    <p class='center' style='border: 1px solid #ccc; background: #111; padding: 5px; margin-bottom: 15px;'>
    <b>Online Users:</b><br /><br />
    <span style='font-size: 10px;'>
    <a href='profile.php?id=31' title='View Profile'>seegee</a>, <a href='profile.php?id=30' title='View Profile'>neilvip1</a>, <a href='profile.php?id=42' title='View Profile'>Majin</a>, <a href='profile.php?id=116' title='View Profile'>Judahmang</a>, <a href='profile.php?id=56' title='View Profile'>xjoshgx</a>, <a href='profile.php?id=50' title='View Profile'>Citizen</a></span>
    </p>
     
    <p class='center'>
    Copyright &copy; Atomic Booter<br />
    <a href='tos.php' title='Terms Of Service' target='_blank'>Terms Of Service</a>
    </p>
    </div>
    </div>
    </body>
    </html>
    It would be great if someone could help. Thanks in advance!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: HTML Unit Help

    If this:
    tme.setValueAttribute(Time);
    is throwing a null pointer, then this:
    Time=TimeField.getText();
    must be setting Time to null.
    Clearly TimeField exists, otherwise you'd get an error thrown there instead. The question becomes: when does "getText() return null", which I do not know. Maybe someone else can figure it out from here.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HTML Unit Help

    A quick look at the API for HtmlForm states for the getFirstByXPath method:
    returning the first matching element, or null if no node matches the specified XPath expression.
    Thus given the location of the exception, I'd say that tme is null. I'm not familiar with how this library parses the values, but make sure you are specifying the correct values to parse, and to verify what is null use some println's (based upon the above, the first variable to test would be tme)

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HTML Unit Help

    Printing Time returns the value that I enter into the "TimeField" text box. Printing tme returns null, but what should it return? tme is a text box on an html page. I cant seem to figure this out :/ The only thing I can think of is that it is not keeping me logged into the site, and you need to be logged in to access the second page (hub.php) but it should keep me logged in as I have cookies enabled. Any other ideas?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HTML Unit Help

    Quote Originally Posted by Seegee View Post
    Printing tme returns null
    Work backwards from there. What does printing the other values from that page print? Are they null as well? If not, then your parsing of the page is incorrect. If so, then you should investigate whether the page is loaded correctly.

    Just as I write the advice above, a closer look at your code - presuming I understand it correctly - suggests the variables tme, prt, pwr should be retrieved from form2?

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HTML Unit Help

    Printing tme, pwr, prt, or textField2 all return null. And yes, they come from the XPath inside of the () after them. That XPath is in form 2. I personally think that it is a problem with the cookies. When I disable cookies, it gives me the same exact error. On this site, if you don't have the login cookie, you wont stay logged in and it will just log you out and redirect you to the login page. I think that it is redirecting it to the login page and then java cant find the text boxes and buttons because they are not on the login page, therefore making them null.
    Last edited by Seegee; April 2nd, 2012 at 08:06 PM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: HTML Unit Help

    Print out the page that the site is returning and inspect the source (I presume the API you are using has methods of doing this). That being said, what I meant by the above post:
    final HtmlTextInput tme = form.getFirstByXPath("/html/body/div/div[6]/form/table/tbody/tr[4]/td[2]/input");
    Do you want this to refer to form2 rather than form?

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HTML Unit Help

    Quote Originally Posted by copeg View Post
    Print out the page that the site is returning and inspect the source (I presume the API you are using has methods of doing this). That being said, what I meant by the above post:
    final HtmlTextInput tme = form.getFirstByXPath("/html/body/div/div[6]/form/table/tbody/tr[4]/td[2]/input");
    Do you want this to refer to form2 rather than form?
    God im stupid. I cant believe i didnt change form to form 2. Well anyway, that got rid of the errors but its still not working. The site is something I use to stress test servers and connections and it is not stressing the ip that I put yet when I do it from the actual site, it does. I get no output now.

Similar Threads

  1. HTML Parsing
    By Bacon n' Logic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2012, 06:18 PM
  2. Hoe do I read html from the web?
    By bholzer in forum Other Programming Languages
    Replies: 2
    Last Post: August 9th, 2011, 06:09 PM
  3. HTML Help Please!!!!!!
    By coolteenaaron in forum Other Programming Languages
    Replies: 1
    Last Post: May 25th, 2011, 04:28 PM
  4. HTML in JOptionPane
    By LordThumper in forum AWT / Java Swing
    Replies: 1
    Last Post: April 27th, 2011, 11:50 AM
  5. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM