12 Replies - 2388 Views - Last Post: 08 November 2011 - 06:32 PM Rate Topic: -----

#1 hateborne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-September 10

Exception in thread "main" java.lang.NullPointerException (pac

Posted 03 November 2011 - 02:07 PM

Sorry, I broke it again.

I cannot seem to figure out why it's dying on this:
public logParser()
    {
        //JFrame homeFrame = new JFrame();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("- IPFW Parser -");
        setSize(1080,480);
        setLayout(new BorderLayout());
               
        // panels
        JPanel panelN = new JPanel();
        JPanel panelS = new JPanel();
        JPanel panelW = new JPanel();
        Box panelE = Box.createVerticalBox();
        Box panelC = Box.createVerticalBox();
                
        // buttons
        JButton nBrowse = new JButton("Browse");
        nBrowse.addActionListener(new BrowseButtonListener());
        JButton nParse = new JButton("Parse");
        nParse.addActionListener(new ParseButtonListener());
        JButton eSSH = new JButton("SSH");
        eSSH.addActionListener(new SSHButtonListener());
        JButton eSMB = new JButton("SMB");
        eSMB.addActionListener(new SMBButtonListener());
        JButton eMSSQL = new JButton("MSSQL");
        eMSSQL.addActionListener(new MSSQLButtonListener());
        JButton eSQL = new JButton("SQL");
        eSQL.addActionListener(new SQLButtonListener());
        JButton eARD = new JButton("ARD");
        eARD.addActionListener(new ARDButtonListener());
        JButton eWRD = new JButton("WRD");
        eWRD.addActionListener(new WRDButtonListener());
        JButton eTOP5 = new JButton("Top 5");
        eTOP5.addActionListener(new TOP5ButtonListener());
        JButton eOther = new JButton("Other");
        eOther.addActionListener(new OtherButtonListener());
        
        // misc
        JTextField filePath = new JTextField(15);
        filePath.setEditable(false);
        filePath.setText("/var/log/ipfw.log");
        
        // table
        table = new JTable(tableData, tableCol);
        JScrollPane tableScroll = new JScrollPane(table);
        //table.setFillsViewportHeight(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col = null;
        for (int i = 0; i < 8; i++)
        {
            col = table.getColumnModel().getColumn(i);
            
            switch (i)
            {
                case 1:
                    col.setPreferredWidth(2);
                    break;
                case 2:
                    col.setPreferredWidth(1);
                    break;
                case 3:
                    col.setPreferredWidth(4);
                    break;
                case 4:
                    col.setPreferredWidth(2);
                    break;
                case 5:
                    col.setPreferredWidth(4);
                    break;
                case 6:
                    col.setPreferredWidth(2);
                    break;
                case 7:
                    col.setPreferredWidth(4);
                    break;
                case 8:
                    col.setPreferredWidth(2);
                    break;                    
            }
        }
      
        // north panel
        panelN.add(filePath);
        panelN.add(nBrowse);
        panelN.add(nParse);
        
        // east panel
        panelE.add(eTOP5);
        panelE.add(eARD);
        panelE.add(eMSSQL);
        panelE.add(eSMB);
        panelE.add(eSQL);
        panelE.add(eSSH);
        panelE.add(eWRD);
        panelE.add(eOther); 
        
        // center panel
        panelC.add(table.getTableHeader(), BorderLayout.CENTER);
        panelC.add(table);
        
        
        // add panels to frame
        add(panelN, BorderLayout.NORTH);
        add(panelC, BorderLayout.CENTER);
        add(panelE, BorderLayout.EAST);
        add(panelS, BorderLayout.SOUTH);
        add(panelW, BorderLayout.WEST);
        
        
        // finishing touches
        pack();
        setVisible(true);    
    }



It is dying on either the pack(); or setVisible(true);. Giving:
Exception in thread "main" java.lang.NullPointerException
at logparser.logParser.<init>(logParser.java:84)
at logparser.logParser.main(logParser.java:409)
Java Result: 1

BUILD SUCCESSFUL (total time: 2 seconds)

Line 84 is the pack();, and 409 is the new logParser(); line below:
public static void main(String[] args) { 
        buildConnections();
        logParser lp = new logParser();
}



Also, for to prevent any "LOLUWRONG" the class is logParser.java and the public class does extend JFrame.


Thanks again Dreamers.

-Hate

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException (pac

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 03 November 2011 - 02:12 PM

There is no way line 84 can be pack(). The pack() call is after line 84 in this method. Post all your code.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 03 November 2011 - 02:12 PM

It is dying at this line

at logparser.logParser.<init>(logParser.java:84)
called by
at logparser.logParser.main(logParser.java:409)

but if you do not post all your code, including import statement and instance variable definitions, we won't be able to match the line number of the trace with the actual line of code
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 03 November 2011 - 02:21 PM

And this is not only horrible but wrong

        for (int i = 0; i < 8; i++)
        {
            col = table.getColumnModel().getColumn(i);
            
            switch (i)
            {
                case 1:
                    col.setPreferredWidth(2);
                    break;
                case 2:
                    col.setPreferredWidth(1);
                    break;
                case 3:
                    col.setPreferredWidth(4);
                    break;
                case 4:
                    col.setPreferredWidth(2);
                    break;
                case 5:
                    col.setPreferredWidth(4);
                    break;
                case 6:
                    col.setPreferredWidth(2);
                    break;
                case 7:
                    col.setPreferredWidth(4);
                    break;
                case 8:
                    col.setPreferredWidth(2);
                    break;                    
            }


your case: statement should go from 0 to 7 not 1 to 8
but better to do it that way
	int[] width = {2, 1, 4, 2, 4, 2, 4, 2};
	for(int i = 0; i < 8; ++i) {
            col = table.getColumnModel().getColumn(i);
            col.setPreferredWidth(width[i]);			
	}


Would be easier if you end up with 50 columns :)

Making a switch in a for() loop for statement like uou are doing better to simply put the code
		table.getColumnModel().getColumn(0).setPreferredWidth(2);
		table.getColumnModel().getColumn(1).setPreferredWidth(1);
		table.getColumnModel().getColumn(2).setPreferredWidth(4);
		table.getColumnModel().getColumn(3).setPreferredWidth(2);
		...


Happy coding
Was This Post Helpful? 1
  • +
  • -

#5 hateborne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-September 10

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 06:11 AM

Gah, fine. I am forewarning all who attempt to read this that there are numerous bad practice attempts, illogical statements, and other naughty coding. I am trying to start learning java again after nearly a year (from a scant few weeks of a class). I would ask that if you are going to leave a comment about this mess, at least make it useful. Comments of "HAHAUSUX" will be disregarded as I know it's crap.

package logparser;

import java.sql.*;
import java.io.*;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.*;


/**
 *
 * @author -
 */

public class logParser extends JFrame {

    private static Connection conn;
    private static File file;
    private static Scanner inputFile;
    private static Statement stat;
    private static PreparedStatement prep;
    private static JList offenders;
    private static JTable table;
    private static String[] tableCol = {"Month", "Day", "Time", "Protocol", "Src", "Src Port", "Dst", "Dst Port"};
    private static String[][] tableData;
    private static String dNow;
   
    
    //////////////////////////////////////////////////////////////////////////////
    //      LogParserGUI()     -       creates the UI                           //
    //////////////////////////////////////////////////////////////////////////////
    public logParser()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("- IPFW Parser -");
        setSize(1080,480);
        setLayout(new BorderLayout());
               
        // panels
        JPanel panelN = new JPanel();
        JPanel panelS = new JPanel();
        JPanel panelW = new JPanel();
        Box panelE = Box.createVerticalBox();
        Box panelC = Box.createVerticalBox();
                
        // buttons
        JButton nBrowse = new JButton("Browse");
        nBrowse.addActionListener(new BrowseButtonListener());
        JButton nParse = new JButton("Parse");
        nParse.addActionListener(new ParseButtonListener());
        JButton eSSH = new JButton("SSH");
        eSSH.addActionListener(new SSHButtonListener());
        JButton eSMB = new JButton("SMB");
        eSMB.addActionListener(new SMBButtonListener());
        JButton eMSSQL = new JButton("MSSQL");
        eMSSQL.addActionListener(new MSSQLButtonListener());
        JButton eSQL = new JButton("SQL");
        eSQL.addActionListener(new SQLButtonListener());
        JButton eARD = new JButton("ARD");
        eARD.addActionListener(new ARDButtonListener());
        JButton eWRD = new JButton("WRD");
        eWRD.addActionListener(new WRDButtonListener());
        JButton eTOP5 = new JButton("Top 5");
        eTOP5.addActionListener(new TOP5ButtonListener());
        JButton eOther = new JButton("Other");
        eOther.addActionListener(new OtherButtonListener());
        
        // misc
        JTextField filePath = new JTextField(15);
        filePath.setEditable(false);
        filePath.setText("/var/log/ipfw.log");
        
        // table
        table = new JTable(tableData, tableCol);
        JScrollPane tableScroll = new JScrollPane(table);
        //table.setFillsViewportHeight(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col = null;
                
        int[] width = {2, 1, 4, 2, 4, 2, 4, 2};
	for(int i = 0; i < 8; ++i)
        {
	        col = table.getColumnModel().getColumn(i);
	        col.setPreferredWidth(width[i]);           
	}
      
        // north panel
        panelN.add(filePath);
        panelN.add(nBrowse);
        panelN.add(nParse);
        
        // east panel
        panelE.add(eTOP5);
        panelE.add(eARD);
        panelE.add(eMSSQL);
        panelE.add(eSMB);
        panelE.add(eSQL);
        panelE.add(eSSH);
        panelE.add(eWRD);
        panelE.add(eOther); 
        
        // center panel
        panelC.add(table.getTableHeader(), BorderLayout.CENTER);
        panelC.add(table);
        
        
        // add panels to frame
        add(panelN, BorderLayout.NORTH);
        add(panelC, BorderLayout.CENTER);
        add(panelE, BorderLayout.EAST);
        add(panelS, BorderLayout.SOUTH);
        add(panelW, BorderLayout.WEST);
        
        
        // finishing touches
        pack();
        setVisible(true);    
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //                      Begin ButtonListeners Block                         //
    //////////////////////////////////////////////////////////////////////////////
    private class BrowseButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            JOptionPane.showMessageDialog(null, "No");
        }
    }
    
    private class ParseButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (file == null)
            {
                try
                {
                    file = new File("ipfw.log");
                    inputFile = new Scanner(file);


                    // get the date and hash it for unique tables
                    Date tNow = new Date();
                    //String dNow = "d" + Integer.toString(tNow.hashCode());
                    dNow = "dog";

                    // if table somehow exists, destroy it and create anew
                    stat.executeUpdate("drop table if exists " + dNow + " ;");
                    stat.executeUpdate("create table " + dNow + " (tMonth text, iDay int, tTime text, tType text, tSource text, iSource_port int, tDestination text, iDestination_port int);");
                }
                catch (Exception ex)
                {

                }
            }
            
            // arbitrary try-catch block to get the SQL error to go away
            try
            {
            prep = conn.prepareStatement("insert into " + dNow + "  values (?,?,?,?,?,?,?,?);");


            while (inputFile.hasNextLine())
            {
                StringTokenizer strToken = new StringTokenizer(inputFile.nextLine(), " :");

                if (strToken.countTokens() > 16)
                {
                    int i = 0;
                    String s = "";
                    while (strToken.hasMoreTokens()) 
                    {
                        i += 1;

                        // switch to handle log-to-database mapping
                        switch(i)
                        {
                            case 1:         //month
                                prep.setString(i, strToken.nextToken());
                                break;
                            case 2:         //day
                                prep.setString(i, strToken.nextToken());
                                break;
                            case 3:         //hours
                                s = s + strToken.nextToken().toString();
                                s = s +  ":";
                                break;
                            case 4:         //minutes
                                s = s + strToken.nextToken().toString();
                                s = s + ":";
                                break;
                            case 5:         //seconds
                                s = s + strToken.nextToken().toString();
                                prep.setString(3, s);
                                break;
                            case 6:         //skip
                                strToken.nextToken();
                                break;
                            case 7:         //skip
                                strToken.nextToken();
                                break;
                            case 8:         //skip
                                strToken.nextToken();
                                break;
                            case 9:         //skip
                                strToken.nextToken();
                                break;
                            case 10:        //tcp-udp?                        
                                prep.setString((i-6), strToken.nextToken());
                                break;
                            case 11:        //source
                                prep.setString((i-6), strToken.nextToken());
                                break;
                            case 12:        //source_port
                                prep.setString((i-6), strToken.nextToken());
                                break;
                            case 13:        //destination
                                prep.setString((i-6), strToken.nextToken());
                                break;
                            case 14:        //destination_port
                                prep.setString((i-6), strToken.nextToken());
                                break;
                            case 15:         //skip
                                strToken.nextToken();
                                break;
                            case 16:         //skip
                                strToken.nextToken();
                                break;
                            case 17:         //skip
                                strToken.nextToken();
                                break;
                            default:        //*shrug*
                                if (strToken.nextToken().equalsIgnoreCase("en0"))
                                {

                                }
                        }
                    }
                    // create a batch from these items
                    prep.addBatch();
                }
            }
                // turn off autocommit to allow processing
                // process all batches
                // turn on autocommit and such
                conn.setAutoCommit(false);
                prep.executeBatch();
                conn.setAutoCommit(true);
            }
            catch (Exception ex)
            {
                // nothing here, no one cares...
            }
        }
    }
    
    private class SSHButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
          
        }
    }
    
    private class SMBButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           
        }
    }
    
    private class MSSQLButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           
        }
    }
    
    private class SQLButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    private class ARDButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    private class WRDButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    private class TOP5ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                ResultSet rs = stat.executeQuery("select tSource from " + dNow + " group by tSource order by count(tSource) DESC Limit 5;");
                int numRow = 0;
                while (rs.next())
                {
                   numRow++;
                }
                tableData = new String[numRow][8];

                rs = stat.executeQuery("select tSource from " + dNow + " group by tSource order by count(tSource) DESC Limit 5;");
                numRow = 0;
                while (rs.next())
                {
                   tableData[numRow][5] = rs.getString("tSource");
                   numRow++;
                }
                
                rs.close();
            }
            catch (Exception ex)
            {
                
            }
        }
    }
    
    private class OtherButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //      buildConnections()     -       initiate connections required        //
    //////////////////////////////////////////////////////////////////////////////
    private static void buildConnections()
    {
        try
        {
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:loghunt.db");
            stat = conn.createStatement();
            
        }
        catch (Exception ex)
        {
            
        }
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //      main(String[] args)     -       driver essentially                  //
    //////////////////////////////////////////////////////////////////////////////
    public static void main(String[] args) { 
       
        // game over man
        //rs.close();
        //conn.close();
        
        buildConnections();
        logParser lp = new logParser();
        //new logParser();
    }
}
        



For sake of argument, new list of errors since last post:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.JTable$1.getRowCount(JTable.java:666)
at javax.swing.JTable.getRowCount(JTable.java:2631)
at javax.swing.plaf.basic.BasicTableUI.createTableSize(BasicTableUI.java:1646)
at javax.swing.plaf.basic.BasicTableUI.getMinimumSize(BasicTableUI.java:1672)
at javax.swing.JComponent.getMinimumSize(JComponent.java:1714)
at javax.swing.BoxLayout.checkRequests(BoxLayout.java:463)
at javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:281)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:702)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:907)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:702)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at java.awt.window.pack(window.java:706)
at logparser.logParser.<init>(logParser.java:120)
at logparser.logParser.main(logParser.java:381)



-Hate
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 07:02 AM

Did you try a full creation of an object like the line above it? I take it the commented out line is the error.
Was This Post Helpful? 0
  • +
  • -

#7 hateborne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-September 10

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 07:06 AM

I apologize Fuzzyness but I am unsure to which line you are referring to in your post.

If you are referring to line 381, no it failed with:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.JTable$1.getRowCount(JTable.java:666)
at javax.swing.JTable.getRowCount(JTable.java:2631)
at javax.swing.plaf.basic.BasicTableUI.createTableSize(BasicTableUI.java:1646)
at javax.swing.plaf.basic.BasicTableUI.getMinimumSize(BasicTableUI.java:1672)
at javax.swing.JComponent.getMinimumSize(JComponent.java:1714)
at javax.swing.BoxLayout.checkRequests(BoxLayout.java:463)
at javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:281)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:702)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:907)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:702)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at java.awt.window.pack(window.java:706)
at logparser.logParser.<init>(logParser.java:120)
at logparser.logParser.main(logParser.java:361)


-Hate
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 07:08 AM

Prev post : at logparser.logParser.main(logParser.java:381)
Post just now: at logparser.logParser.main(logParser.java:361)

Which one is it because the code you posted 2 posts up 361 is a blank line... The fact the line number changed tells me the code changed so we need to see the code that you are looking at right now and the code that has the error at 361 not 381.
Was This Post Helpful? 0
  • +
  • -

#9 hateborne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-September 10

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 07:13 AM

package logparser;

import java.sql.*;
import java.io.*;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.*;


/**
 *
 * @author -
 */

public class logParser extends JFrame {

    private static Connection conn;
    private static File file;
    private static Scanner inputFile;
    private static Statement stat;
    private static PreparedStatement prep;
    private static JList offenders;
    private static JTable table;
    private static String[] tableCol = {"Month", "Day", "Time", "Protocol", "Src", "Src Port", "Dst", "Dst Port"};
    private static String[][] tableData;
    private static String dNow;
   
    
    //////////////////////////////////////////////////////////////////////////////
    //      LogParserGUI()     -       creates the UI                           //
    //////////////////////////////////////////////////////////////////////////////
    public logParser()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("- IPFW Parser -");
        setSize(1080,480);
        setLayout(new BorderLayout());
               
        // panels
        JPanel panelN = new JPanel();
        JPanel panelS = new JPanel();
        JPanel panelW = new JPanel();
        Box panelE = Box.createVerticalBox();
        Box panelC = Box.createVerticalBox();
                
        // buttons
        JButton nBrowse = new JButton("Browse");
        nBrowse.addActionListener(new BrowseButtonListener());
        JButton nParse = new JButton("Parse");
        nParse.addActionListener(new ParseButtonListener());
        JButton eSSH = new JButton("SSH");
        eSSH.addActionListener(new SSHButtonListener());
        JButton eSMB = new JButton("SMB");
        eSMB.addActionListener(new SMBButtonListener());
        JButton eMSSQL = new JButton("MSSQL");
        eMSSQL.addActionListener(new MSSQLButtonListener());
        JButton eSQL = new JButton("SQL");
        eSQL.addActionListener(new SQLButtonListener());
        JButton eARD = new JButton("ARD");
        eARD.addActionListener(new ARDButtonListener());
        JButton eWRD = new JButton("WRD");
        eWRD.addActionListener(new WRDButtonListener());
        JButton eTOP5 = new JButton("Top 5");
        eTOP5.addActionListener(new TOP5ButtonListener());
        JButton eOther = new JButton("Other");
        eOther.addActionListener(new OtherButtonListener());
        
        // misc
        JTextField filePath = new JTextField(15);
        filePath.setEditable(false);
        filePath.setText("/var/log/ipfw.log");
        
        // table
        table = new JTable(tableData, tableCol);
        JScrollPane tableScroll = new JScrollPane(table);
        //table.setFillsViewportHeight(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col = null;
                
        int[] width = {2, 1, 4, 2, 4, 2, 4, 2};
	for(int i = 0; i < 8; ++i)
        {
	        col = table.getColumnModel().getColumn(i);
	        col.setPreferredWidth(width[i]);           
	}
      
        // north panel
        panelN.add(filePath);
        panelN.add(nBrowse);
        panelN.add(nParse);
        
        // east panel
        panelE.add(eTOP5);
        panelE.add(eARD);
        panelE.add(eMSSQL);
        panelE.add(eSMB);
        panelE.add(eSQL);
        panelE.add(eSSH);
        panelE.add(eWRD);
        panelE.add(eOther); 
        
        // center panel
        panelC.add(table.getTableHeader(), BorderLayout.CENTER);
        panelC.add(table);
        
        
        // add panels to frame
        add(panelN, BorderLayout.NORTH);
        add(panelC, BorderLayout.CENTER);
        add(panelE, BorderLayout.EAST);
        add(panelS, BorderLayout.SOUTH);
        add(panelW, BorderLayout.WEST);
        
        
        // finishing touches
        pack();
        setVisible(true);    
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //                      Begin ButtonListeners Block                         //
    //////////////////////////////////////////////////////////////////////////////
    private class BrowseButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            JOptionPane.showMessageDialog(null, "No");
        }
    }
    
    private class ParseButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (file == null)
            {
                try
                {
                    file = new File("ipfw.log");
                    inputFile = new Scanner(file);


                    // get the date and hash it for unique tables
                    Date tNow = new Date();
                    //String dNow = "d" + Integer.toString(tNow.hashCode());
                    dNow = "dog";

                    // if table somehow exists, destroy it and create anew
                    stat.executeUpdate("drop table if exists " + dNow + " ;");
                    stat.executeUpdate("create table " + dNow + " (tMonth text, iDay int, tTime text, tType text, tSource text, iSource_port int, tDestination text, iDestination_port int);");
                }
                catch (Exception ex)
                {

                }
            }
            
            // arbitrary try-catch block to get the SQL error to go away
            try
            {
                prep = conn.prepareStatement("insert into " + dNow + "  values (?,?,?,?,?,?,?,?);");


                while (inputFile.hasNextLine())
                {
                    StringTokenizer strToken = new StringTokenizer(inputFile.nextLine(), " :");

                    if (strToken.countTokens() > 16)
                    {
                        // mapping the info to the correct fields
                        try
                        {
                            String s = "";
                            
                            //month
                               prep.setString(1, strToken.nextToken());
                            //day
                               prep.setString(2, strToken.nextToken());
                            //hours
                               s = s + strToken.nextToken().toString();
                               s = s +  ":"; 
                            //minutes
                               s = s + strToken.nextToken().toString();
                               s = s + ":";
                            //seconds
                               s = s + strToken.nextToken().toString();
                               prep.setString(3, s);
                            //skip
                               strToken.nextToken();
                            //skip
                               strToken.nextToken();
                            //skip
                               strToken.nextToken();
                            //skip
                               strToken.nextToken();
                            //tcp-udp?                        
                               prep.setString((4), strToken.nextToken());
                            //source
                               prep.setString((5), strToken.nextToken());
                            //source_port
                               prep.setString((6), strToken.nextToken());
                            //destination
                               prep.setString((7), strToken.nextToken());
                            //destination_port
                               prep.setString((8), strToken.nextToken());
                            //skip
                               strToken.nextToken();
                            //skip
                               strToken.nextToken();
                            //skip
                               strToken.nextToken();
                        }
                        catch (Exception exc)
                        {

                        }

                    }
                        // create a batch from these items
                        prep.addBatch();
                }

                    // turn off autocommit to allow processing
                    // process all batches
                    // turn on autocommit and such
                    conn.setAutoCommit(false);
                    prep.executeBatch();
                    conn.setAutoCommit(true);
            }
            catch (Exception ex)
            {
                // nothing here, no one cares...
            }
        }
    }
    
    private class SSHButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
          
        }
    }
    
    private class SMBButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           
        }
    }
    
    private class MSSQLButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           
        }
    }
    
    private class SQLButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    private class ARDButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    private class WRDButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    private class TOP5ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                ResultSet rs = stat.executeQuery("select tSource from " + dNow + " group by tSource order by count(tSource) DESC Limit 5;");
                int numRow = 0;
                while (rs.next())
                {
                   numRow++;
                }
                tableData = new String[numRow][8];

                rs = stat.executeQuery("select tSource from " + dNow + " group by tSource order by count(tSource) DESC Limit 5;");
                numRow = 0;
                while (rs.next())
                {
                   tableData[numRow][5] = rs.getString("tSource");
                   numRow++;
                }
                
                rs.close();
            }
            catch (Exception ex)
            {
                
            }
        }
    }
    
    private class OtherButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //      buildConnections()     -       initiate connections required        //
    //////////////////////////////////////////////////////////////////////////////
    private static void buildConnections()
    {
        try
        {
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:loghunt.db");
            stat = conn.createStatement();
            
        }
        catch (Exception ex)
        {
            
        }
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //      main(String[] args)     -       driver essentially                  //
    //////////////////////////////////////////////////////////////////////////////
    public static void main(String[] args) { 
       
        //JOptionPane.showMessageDialog(null,"Works yo");
        // game over man
        //rs.close();
        //conn.close();
        
        buildConnections();
        //logParser lp = new logParser();
        new logParser();
    }
}
        



Exception in thread "main" java.lang.NullPointerException
at javax.swing.JTable$1.getRowCount(JTable.java:666)
at javax.swing.JTable.getRowCount(JTable.java:2631)
at javax.swing.plaf.basic.BasicTableUI.createTableSize(BasicTableUI.java:1646)
at javax.swing.plaf.basic.BasicTableUI.getMinimumSize(BasicTableUI.java:1672)
at javax.swing.JComponent.getMinimumSize(JComponent.java:1714)
at javax.swing.BoxLayout.checkRequests(BoxLayout.java:463)
at javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:281)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:702)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:907)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:702)
at java.awt.Container.preferredSize(Container.java:1599)
at java.awt.Container.getPreferredSize(Container.java:1584)
at java.awt.window.pack(window.java:706)
at logparser.logParser.<init>(logParser.java:120)
at logparser.logParser.main(logParser.java:361)


I was tinkering with it. Apologies

-Hate
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 08:23 AM

Look at panelC- it is a Box. Why are you dealing with BorderLayout constraints here?
panelC.add(table.getTableHeader(), BorderLayout.CENTER);
panelC.add(table);



I played around with your code, and for some reason, commenting out panelC.add(table); caused the Exception to go away.

Honestly though, as you said in one of your earlier posts, your design needs work. Static has its uses, but should be used sparingly. The static keyword means that the element is associated with the class, not the object. Your global variables should really be instance fields.

Also, don't leave your catch blocks empty. At least printStackTrace() on them so you get a stack trace to help you debug.
Was This Post Helpful? 0
  • +
  • -

#11 hateborne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-September 10

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 08:34 AM

Thank you for the reply. I am seeing more and more that programming is a lost cause for me.

I will tinker some more and rewrite decent chunks.

Thank You.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 04 November 2011 - 08:37 AM

Programming isn't easy; but if you don't mind my saying, maybe you're biting off too much too soon. How strong is your OOP? You should take a step back and hit hard on that. Leave the GUI stuff alone until you have more experience. My thread Getting Better at Programming Java should help you get a better idea of what you might want to brush up on before tackling GUI programs. :)
Was This Post Helpful? 0
  • +
  • -

#13 hateborne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-September 10

Re: Exception in thread "main" java.lang.NullPointerException (pac

Posted 08 November 2011 - 06:32 PM

After not touching it for a few days, I found quite a few things and fixed them. There are still quite a few things that are bad but less so (at least to the best of my knowledge).

I have cleaned it as best I can in 15min, along with moderate insight into whatever the hell I was thinking when I started this monster.

Thank you again for any insight offered.

main.java
package logparser;

/**
 *
 * @author -
 */

public class logParser {

    //////////////////////////////////////////////////////////////////////////////
    //      main(String[] args)     -       driver essentially                  //
    //////////////////////////////////////////////////////////////////////////////
    public static void main(String[] args) { 
       
        new logParserUI();        
    }
}



logParserUI.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logparser;
import java.sql.*;
import java.util.Date;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 */
public class logParserUI extends JFrame implements WindowListener {
    
    private logParserBG bg = new logParserBG();
    private JTable table;
    private String[] tableCol = {"Month", "Day", "Time", "Protocol", "Src", "Src Port", "Dst", "Dst Port"};
    public static String[][] tableData = {{""},{""}};
    private JPanel panelN, panelS, panelW;
    private Box panelE, panelC;
    private JTextField filePath;
    
    public logParserUI()
    {
        // initiate connections
        bg.buildConnections();
        startPOS();


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("- IPFW Parser -");
        setSize(1080,300);
        setLayout(new BorderLayout());

        // build panels               
        buildNorthPanel();
        buildSouthPanel();
        buildWestPanel();
        buildCenterPanel();
        buildEastPanel();        


        // add panels to frame
        add(panelN, BorderLayout.NORTH);
        add(panelC, BorderLayout.CENTER);
        add(panelE, BorderLayout.EAST);
        add(panelS, BorderLayout.SOUTH);
        add(panelW, BorderLayout.WEST);


        // finishing touches
        this.addWindowListener(this);
        setVisible(true);
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //                   Window Listener Event Overrides                        //
    //////////////////////////////////////////////////////////////////////////////
    public void windowDeactivated(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
    public void windowActivated(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
    public void windowClosing(WindowEvent e){}
    public void windowClosed(WindowEvent e)
    {
        bg.clean();
    }

    //////////////////////////////////////////////////////////////////////////////
    //      sizeTable(int)    -     size the table's Columns appropriately      //        
    //////////////////////////////////////////////////////////////////////////////
    public void sizeTable(int rC)
    {
        // size the columns accordingly
        TableColumn col = null;
        int[] width = {2, 1, 4, 2, 4, 2, 4, 2};
        for(int i = 0; i < 8; ++i)
        {
	    col = table.getColumnModel().getColumn(i);
	    col.setPreferredWidth(width[i]);           
	}
        
        // ?
        table = new JTable(tableData, tableCol);
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //      startPOS()     -       set "-" across the board                     //        
    //////////////////////////////////////////////////////////////////////////////
    private void startPOS()
    {
        // generic fill in data   
        tableData = new String[1][8];

        for (int i = 0; i < 8; i++)
        {
           tableData[0][i] = "-";
        }

    }
    
    //////////////////////////////////////////////////////////////////////////////
    //      setTableRows(String)    -      take the passed SQL query, run it,   //
    //                                     and resize the tableData array       //
    //                                     accordingly.                         //        
    //////////////////////////////////////////////////////////////////////////////
    public void setTableRows(String Query)
    {
        // 
        int rC = bg.prepTable(Query);
        tableData = new String[rC][8];
    }

    
    //////////////////////////////////////////////////////////////////////////////
    //                      Begin Panels Building Block                         //
    //////////////////////////////////////////////////////////////////////////////
    public void buildNorthPanel()
    {
        panelN = new JPanel();
        
        // buttons
        JButton nBrowse = new JButton("Browse");
        nBrowse.addActionListener(new BrowseButtonListener());
        JButton nParse = new JButton("Parse");
        nParse.addActionListener(new ParseButtonListener());
        
        //misc
        filePath = new JTextField(25);
        filePath.setEditable(false);
        filePath.setText("/var/log/ipfw.log");
        
        // add to panel
        panelN.add(filePath);
        panelN.add(nBrowse);
        panelN.add(nParse);
        
    }
    
    private void buildSouthPanel()
    {
        panelS = new JPanel();
    }
    
    private void buildWestPanel()
    {
        panelW = new JPanel();
    }
    
    private void buildEastPanel()
    {
        panelE = Box.createVerticalBox();
        
        // buttons
        JButton eSSH = new JButton("SSH");
        eSSH.addActionListener(new SSHButtonListener());
        JButton eSMB = new JButton("SMB");
        eSMB.addActionListener(new SMBButtonListener());
        JButton eMSSQL = new JButton("MSSQL");
        eMSSQL.addActionListener(new MSSQLButtonListener());
        JButton eSQL = new JButton("SQL");
        eSQL.addActionListener(new SQLButtonListener());
        JButton eARD = new JButton("ARD");
        eARD.addActionListener(new ARDButtonListener());
        JButton eWRD = new JButton("WRD");
        eWRD.addActionListener(new WRDButtonListener());
        JButton eTOP5 = new JButton("Top 5");
        eTOP5.addActionListener(new TOP5ButtonListener());
        JButton eOther = new JButton("Other");
        eOther.addActionListener(new OtherButtonListener());
        
        // add to panel
        panelE.add(eTOP5);
        panelE.add(eARD);
        panelE.add(eMSSQL);
        panelE.add(eSMB);
        panelE.add(eSQL);
        panelE.add(eSSH);
        panelE.add(eWRD);
        panelE.add(eOther);
    }
    
    private void buildCenterPanel()
    {
        panelC = Box.createVerticalBox();
        
        table = new JTable(tableData, tableCol);
        JScrollPane tableScroll = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col = null;
                
        int[] width = {2, 1, 4, 2, 4, 2, 4, 2};
	for(int i = 0; i < 8; ++i)
        {
	        col = table.getColumnModel().getColumn(i);
	        col.setPreferredWidth(width[i]);           
	}
        
        panelC.add(table.getTableHeader());
        panelC.add(table);
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //                      Begin ButtonListeners Block                         //
    //////////////////////////////////////////////////////////////////////////////
    private class BrowseButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            JFileChooser fc = new JFileChooser();
            int status = fc.showOpenDialog(null);
            if (status == JFileChooser.APPROVE_OPTION)
            {
                bg.setFile(fc.getSelectedFile());
                filePath.setText(bg.getFile().toString());
            }
        }
    }
    
    // load selected file and parse it
    private class ParseButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            bg.parse();
        }
    }
    
    // sort data by SSH (port 22)
    private class SSHButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            // rowCount variable + initialization
            int rowCount = 0;
            // the SQL query
            String Query = "select * from " + bg.getDB() + " where iDestination_port = 22;";
            // find the number of rows needed
            setTableRows(Query);
            // hopefully set the tableData
            tableData = bg.ssh(tableData, Query);
            // get the rowCount for sizeTable call
            rowCount = bg.getRowCount();
            // resize the columns and finish the table (?)
            sizeTable(rowCount);
        }
    }
    
    // sort data by SMB (port 445)
    private class SMBButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           
        }
    }
    
    // sort data by MSSQL (port 1433)
    private class MSSQLButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           
        }
    }
    
    // sort data by SQL (port 3306)
    private class SQLButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    // sort data by ARD (ports 3283 and 5900)
    private class ARDButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    // sort data by WRD (port 3389)
    private class WRDButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    // show the top 5 offenders based on count
    private class TOP5ButtonListener implements ActionListener
    {
        //////////////////////////////// IGNORE
        //////////////////////////////// IGNORE 
        public void actionPerformed(ActionEvent e)
        {
//            // data access
//            String dNow = bg.getDB();
//            Statement stat = bg.getStat();            
//            
//            try
//            {
//                // row count pass, wasteful but sadly necessary
//                ResultSet rs = stat.executeQuery("select tSource from " + dNow + " group by tSource order by count(tSource) DESC Limit 5;");
//                int numRow = 0;
//                while (rs.next())
//                {
//                   numRow++;
//                }
//                tableData = new String[numRow][8];
//
//                rs = stat.executeQuery("select tSource from " + dNow + " group by tSource order by count(tSource) DESC Limit 5;");
//                numRow = 0;
//                while (rs.next())
//                {
//                   tableData[numRow][5] = rs.getString("tSource");
//                   numRow++;
//                }
//                
//                rs.close();
//            }
//            catch (Exception ex)
//            {
//                //ex.printStackTrace();
//            }
        }
        //////////////////////////////// IGNORE
        //////////////////////////////// IGNORE
    }
    
    // search for a specific port in log
    private class OtherButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    
}



logParserBG.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logparser;

import java.sql.*;
import java.io.*;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 */
public class logParserBG {
    
    private Connection conn;
    private File file;
    private Scanner inputFile;
    private static Statement stat;
    private PreparedStatement prep;
    private String dNow;
    public int rowCount;
    
    
    //////////////////////////////////////////////////////////////////////////////
    //    "get-" statements to return necessary information to other classes    //
    //////////////////////////////////////////////////////////////////////////////
    public String getDB()
    {
        return dNow;
    }
    
    public Statement getStat()
    {
        return stat;
    }
    
    public PreparedStatement getPrep()
    {
        return prep;
    }
    
    public Connection getConn()
    {
        return conn;
    }
    
    public File getFile()
    {
        return file;
    }
    
    public Scanner getInputFile()
    {
        return inputFile;
    }
    
    public int getRowCount()
    {
        return rowCount;
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //    "set-" statements to return necessary information to other classes    //
    //////////////////////////////////////////////////////////////////////////////
    public void setFile(File f)
    {
        file = f;
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //      buildConnections()     -       initiate connections required        //
    //////////////////////////////////////////////////////////////////////////////
    public void buildConnections()
    {
        try
        {
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:loghunt.db");
            stat = conn.createStatement();
            
            // get the date and hash it for unique tables
            Date tNow = new Date();
            //String dNow = "d" + Integer.toString(tNow.hashCode());
            dNow = "dog";

            // if table somehow exists, destroy it and create anew
            stat.executeUpdate("drop table if exists " + dNow + " ;");
            stat.executeUpdate("create table " + dNow + " (tMonth text, iDay int, tTime text, tType text, tSource text, iSource_port int, tDestination text, iDestination_port int);");
            
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //      clean()     -       wipe out everything and close program           //
    //////////////////////////////////////////////////////////////////////////////
    public void clean()
    {
        try
        {
            conn.close();
            
            System.exit(0);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //      parse()     -       pull in and parse data from file                //
    //////////////////////////////////////////////////////////////////////////////
    public void parse()
    {
        try
        {
            inputFile = new Scanner(file);

            // get the date and hash it for unique tables
            Date tNow = new Date();
            //String dNow = "d" + Integer.toString(tNow.hashCode());
            dNow = "dog";
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        

        // arbitrary try-catch block to get the SQL error to go away
        try
        {
            prep = conn.prepareStatement("insert into " + dNow + "  values (?,?,?,?,?,?,?,?);");


            while (inputFile.hasNextLine())
            {
                StringTokenizer strToken = new StringTokenizer(inputFile.nextLine(), " :");

                if (strToken.countTokens() > 16)
                {
                    // mapping the info to the correct fields
                    try
                    {
                        String s = "";

                        //month
                           prep.setString(1, strToken.nextToken());
                        //day
                           prep.setString(2, strToken.nextToken());
                        //hours
                           s = s + strToken.nextToken().toString();
                           s = s +  ":"; 
                        //minutes
                           s = s + strToken.nextToken().toString();
                           s = s + ":";
                        //seconds
                           s = s + strToken.nextToken().toString();
                           prep.setString(3, s);
                        //skip
                           strToken.nextToken();
                        //skip
                           strToken.nextToken();
                        //skip
                           strToken.nextToken();
                        //skip
                           strToken.nextToken();
                        //tcp-udp?                        
                           prep.setString((4), strToken.nextToken());
                        //source
                           prep.setString((5), strToken.nextToken());
                        //source_port
                           prep.setString((6), strToken.nextToken());
                        //destination
                           prep.setString((7), strToken.nextToken());
                        //destination_port
                           prep.setString((8), strToken.nextToken());
                        //skip
                           strToken.nextToken();
                        //skip
                           strToken.nextToken();
                        //skip
                           strToken.nextToken();
                    }
                    catch (Exception exc)
                    {
                        exc.printStackTrace();
                    }

                }
                    // create a batch from these items
                    prep.addBatch();
            }

                // turn off autocommit to allow processing
                // process all batches
                // turn on autocommit and such
                conn.setAutoCommit(false);
                prep.executeBatch();
                conn.setAutoCommit(true);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    //////////////////////////////////////////////////////////////////////////////
    //      ssh()     -       accept td (tableData) and the SQL query,          //
    //                        set/sort the data into the correct fields         //        
    //////////////////////////////////////////////////////////////////////////////
    public String[][] ssh(String[][] td, String Query)
    {
            // data access
            try
            {
                ResultSet rs = stat.executeQuery(Query);
                
                int numRow = 0;
                while (rs.next())
                {
                   td[numRow][0] = rs.getString("tMonth");
                   td[numRow][1] = rs.getString("iDay");
                   td[numRow][2] = rs.getString("tTime");
                   td[numRow][3] = rs.getString("tType");
                   td[numRow][4] = rs.getString("tSource");
                   td[numRow][5] = rs.getString("iSource_port");
                   td[numRow][6] = rs.getString("tDestination");
                   td[numRow][7] = rs.getString("iDestination_port");
                   numRow++;
                }
                
                rs.close();

            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
           
            return td;
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    //     prepTable(String)  -  accept the SQL query, run a ghetto row count,  //
    //                           set the rowCount field, and return it          //        
    //////////////////////////////////////////////////////////////////////////////
    public int prepTable(String Query)
    {
        try
        {
            ResultSet rs = stat.executeQuery(Query);
            int numRow = 0;
            while (rs.next())
            {
               numRow++;
            }
            rowCount = numRow;
            
            rs.close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        
       
        return rowCount;
    }
    
}



The more I tinker, the more I realize that subnetting/basic-garbage-scripting is as close to programming as I may ever get. :-P

-Hate
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1