-5

I am trying to get some info from a website

I wrote code for the same.

Please have a look at the code and point me where I am doing wrong.

UPDATE:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String site_url = jComboBox1.getSelectedItem().toString();
        try {
            String site_source = getUrlSource(site_url);
            System.out.println(site_source.length());

            Pattern p = Pattern.compile("\\b(pin)\\w*()\\b");
            Matcher m = p.matcher(site_source);

            int index = 1;
            char strat;
            String array_pin[] = null; 
            array_pin[0] = "";
            while (m.find()) { // find next match
                String match = m.group();

                if(match.equals("pin")){
                    System.out.println(match);

                    for(int i = m.end()+1;i<m.end()+20;i++){
                        strat = site_source.charAt(i);
                        if(Character.isDigit(strat)){
                            if(array_pin == null){
                                String strat22 = ""+strat;
                                System.out.println(strat22);
                                array_pin[index] = strat22;
                            }else{
                                array_pin[index] = array_pin[index]+""+ strat;
                            }

                        }    
                    }

                }

                index++;
            }

            // false because regex does not match the whole string
            System.out.println("Matches: " + m.matches());


        } catch (IOException ex) {
            Logger.getLogger(mainwind.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    

When I run the code I get following error:

    run:
25713
pin
pin
pin
pin
pin
7
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at codepin.mainwind.jButton1ActionPerformed(mainwind.java:159)
    at codepin.mainwind.access$100(mainwind.java:22)
    at codepin.mainwind$2.actionPerformed(mainwind.java:89)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 6 minutes 20 seconds)

What all I need to do to get rid off this issue ?

istshrna istshrna
  • 111
  • 1
  • 1
  • 6

1 Answers1

0

You initialize this array variable with null:

String array_pin[] = null;

Then you try to assign to elements of the array, the array that doesn't exist because the variable is null:

array_pin[index] = strat22;

Boom! NullPointerException!

So you need to create an array first by assigning something other than null:

String array_pin[] = new String[100];

(where 100, for example, is the size of the array).

Note that array indexes start at 0, not 1, so your index variable should start at 0 too.

See Arrays in the Java tutorials.

P.S. An array is not great here, because arrays are created with a fixed size, and you don't know how in advance how many elements you will need. You would be better off with a List:

ArrayList<String> pins = new ArrayList<>();
...
pins.add(strat22);

(import java.util.ArrayList if not already)

Then you do not need the index variable either.

Boann
  • 48,794
  • 16
  • 117
  • 146