Help with Split/Join String problems
in
Programming Questions
•
1 year ago
Hi I am having some trouble getting my code to run and think it may have to do with my use of the join() function. But first some background on what I am trying to accomplish...
Currently I receive an e-mail once an hour with minute resolution data. I would like to combined the data contained in 24 (or more) e-mails into one large csv file. Part of the problem is that the body of each e-mail(no attachments) has a header before the data is presented in csv format (See bottom for example). In the following code you can see that I am trying to split the message string, into a string array that separates the message string at each '\n' AKA carriage return (Credit to Phi.lho for the idea). Next I try and join the strings contained in that string array back together minus the first 8 lines as that is where the header is.
As it completes each edited message they are added to a different string array of all the edited messages. I then try and join these together with '\n' as the delimiter. Lastly I am simply trying to print the resulting string so that I can verify the data looks correct before I move to the next step of actually creating a text file.
My problem is that I keep receiving a Java Null pointer error (along with some others) and can't figure out why. Do strings have a limit to how long they can be? Maybe I'm not using the join() function properly? Any help or suggestions would be much appreciated.
Code:
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.mail.Authenticator;
- import javax.mail.PasswordAuthentication;
- String []target;
- String []parsedMess;
- String []editedMess;
- String completeMess;
- void setup() {
- size(200,200);
- checkMail();
- System.out.println(completeMess);
- noLoop();
- }
- /****************************************************************CHECK MAIL FUNCTION***********************************************************************************/
- void checkMail() {
- try {
- Properties props = System.getProperties();
- props.put("mail.pop3.host", "pop.gmail.com");
- // These are security settings required for gmail
- // May need different code depending on the account
- props.put("mail.pop3.port", "995");
- props.put("mail.pop3.starttls.enable", "true");
- props.setProperty("mail.pop3.socketFactory.fallback", "false");
- props.setProperty("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");
- // Create authentication object
- Auth auth = new Auth();
- // Make a session
- Session session = Session.getDefaultInstance(props, auth);
- Store store = session.getStore("pop3");
- store.connect();
- // Get inbox
- Folder folder = store.getFolder("INBOX");
- folder.open(Folder.READ_ONLY);
- System.out.println(folder.getMessageCount() + " total messages.");
- // Get array of messages and display them
- Message message[] = folder.getMessages();
- for (int i=0; i < message.length; i++) {
- int y = 0;
- String content = message[i].getContent().toString();
- String[] emailMess = split(content, '\n' ); //Here the email body is split up into sections seperated by a carriage return
- for (int k = 7; k < emailMess.length; k++){ //I start at 7 here to remove the unwanted lines at the top of each e-mail
- parsedMess[y] = emailMess[k]; //Create parsedMess array using email array minus the first 7 lines
- y++;
- }
- editedMess[i] = join(parsedMess,'\n'); //Join my strings back together seperated by a carriage return (as it was originally) to form one large email string
- }
- completeMess = join(editedMess, '\n'); //Join all my complete email strings together into one large
- // Close the session
- folder.close(false);
- store.close();
- }
- // This error handling isn't very good
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public class Auth extends Authenticator {
- public Auth() {
- super();
- }
- public PasswordAuthentication getPasswordAuthentication() {
- String username, password;
- username = "mymail@gmail.com";
- password = "password";
- System.out.println("authenticating. . ");
- return new PasswordAuthentication(username, password);
- }
- }
Error Message:
authenticating. .6 total messages.java.lang.NullPointerExceptionnullat JavaMail_Test2.checkMail(JavaMail_Test2.java:91)at JavaMail_Test2.setup(JavaMail_Test2.java:52)at processing.core.PApplet.handleDraw(Unknown Source)at processing.core.PApplet.run(Unknown Source)at java.lang.Thread.run(Thread.java:662)
Example E-mail Body:
LogName:EgyDmd Log
DeviceType:9999
SerialNumber:BP-9999F444-11
Owner:Me
Tag1:SomeTag
Tag2:Fixed
RecordNumber, UTC, kWh imp, kWh exp, kWh net, kVARh imp, kVARh exp, kVARh net, kVAh tot, kW swd, kVAR swd, kVA swd, I avg swd, PF sign mean, Vln a, Vln b
786049, 1329912000, 2.8563, 2283.1353, -2280.2791, 0.0000, 1694.6766, -1694.6766, 3443.8826, 0.0002, -0.1177, 0.1177, 0.4806, 0.1281, 124.3940, 124.3312
786048, 1329911940, 2.8563, 2283.1353, -2280.2791, 0.0000, 1694.6747, -1694.6747, 3443.8804, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 124.3008, 124.0223
786047, 1329911880, 2.8563, 2283.1353, -2280.2791, 0.0000, 1694.6726, -1694.6726, 3443.8784, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.3276, 122.0365
786046, 1329911820, 2.8563, 2283.1353, -2280.2791, 0.0000, 1694.6707, -1694.6707, 3443.8765, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.3404, 122.3035
786045, 1329911760, 2.8562, 2283.1353, -2280.2791, 0.0000, 1694.6687, -1694.6687, 3443.8745, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.4645, 122.2765
786044, 1329911700, 2.8562, 2283.1353, -2280.2791, 0.0000, 1694.6667, -1694.6667, 3443.8726, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.4812, 122.3815
786043, 1329911640, 2.8562, 2283.1353, -2280.2791, 0.0000, 1694.6648, -1694.6648, 3443.8706, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.4334, 122.2712
786042, 1329911580, 2.8562, 2283.1353, -2280.2791, 0.0000, 1694.6628, -1694.6628, 3443.8687, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.3766, 122.2283
786041, 1329911520, 2.8562, 2283.1353, -2280.2791, 0.0000, 1694.6609, -1694.6609, 3443.8667, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.3378, 122.1680
786040, 1329911460, 2.8562, 2283.1353, -2280.2791, 0.0000, 1694.6589, -1694.6589, 3443.8647, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.2749, 122.0662
786039, 1329911400, 2.8562, 2283.1353, -2280.2791, 0.0000, 1694.6570, -1694.6570, 3443.8628, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.1977, 122.0540
786038, 1329911340, 2.8562, 2283.1350, -2280.2791, 0.0000, 1694.6550, -1694.6550, 3443.8608, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.2357, 121.9763
786037, 1329911280, 2.8562, 2283.1350, -2280.2791, 0.0000, 1694.6531, -1694.6531, 3443.8589, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.1374, 122.0565
786036, 1329911220, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6511, -1694.6511, 3443.8569, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.2039, 122.0367
786035, 1329911160, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6492, -1694.6492, 3443.8550, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.1499, 122.0929
786034, 1329911100, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6472, -1694.6472, 3443.8530, -0.0002, -0.1164, 0.1164, 0.4778, -0.1562, 122.3659, 122.2163
786033, 1329911040, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6453, -1694.6453, 3443.8511, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 122.4191, 122.1929
786032, 1329910980, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6433, -1694.6433, 3443.8491, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 122.4080, 122.2509
786031, 1329910920, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6414, -1694.6414, 3443.8472, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 122.4865, 122.3992
786030, 1329910860, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6394, -1694.6394, 3443.8452, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 122.6811, 122.5503
786029, 1329910800, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6375, -1694.6375, 3443.8433, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 122.4628, 122.3496
786028, 1329910740, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6355, -1694.6355, 3443.8413, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 122.5921, 122.2466
786027, 1329910680, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6335, -1694.6335, 3443.8394, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.4974, 121.1583
786026, 1329910620, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6316, -1694.6316, 3443.8374, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.6127, 121.3067
786025, 1329910560, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6298, -1694.6298, 3443.8354, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.6853, 121.3199
786024, 1329910500, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6278, -1694.6278, 3443.8337, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.5139, 121.4742
786023, 1329910440, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6259, -1694.6259, 3443.8318, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.5484, 121.3845
786022, 1329910380, 2.8561, 2283.1350, -2280.2791, 0.0000, 1694.6239, -1694.6239, 3443.8298, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.4636, 121.3107
786021, 1329910320, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6219, -1694.6219, 3443.8279, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.5711, 121.3501
786020, 1329910260, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6201, -1694.6201, 3443.8259, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.3626, 121.2934
786019, 1329910200, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6182, -1694.6182, 3443.8240, 0.0002, -0.1160, 0.1160, 0.4775, 0.1527, 121.6177, 121.3028
786018, 1329910140, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6162, -1694.6162, 3443.8220, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.6762, 121.5345
786017, 1329910080, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6143, -1694.6143, 3443.8201, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.5700, 121.4047
786016, 1329910020, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6123, -1694.6123, 3443.8181, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.4440, 121.3411
786015, 1329909960, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6104, -1694.6104, 3443.8162, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.3713, 121.1934
786014, 1329909900, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6084, -1694.6084, 3443.8142, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.4911, 121.4139
786013, 1329909840, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6066, -1694.6066, 3443.8123, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.6549, 121.5087
786012, 1329909780, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6046, -1694.6046, 3443.8105, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.5619, 121.5203
786011, 1329909720, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6027, -1694.6027, 3443.8086, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.6864, 121.3358
786010, 1329909660, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.6007, -1694.6007, 3443.8066, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.6021, 121.3944
786009, 1329909600, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.5988, -1694.5988, 3443.8047, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.7033, 121.6300
786008, 1329909540, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.5968, -1694.5968, 3443.8027, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.6917, 121.4766
786007, 1329909480, 2.8560, 2283.1350, -2280.2791, 0.0000, 1694.5950, -1694.5950, 3443.8008, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.7781, 121.6066
786006, 1329909420, 2.8559, 2283.1348, -2280.2791, 0.0000, 1694.5930, -1694.5930, 3443.7988, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.3644, 121.2071
786005, 1329909360, 2.8559, 2283.1348, -2280.2791, 0.0000, 1694.5911, -1694.5911, 3443.7969, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.3804, 121.2030
786004, 1329909300, 2.8559, 2283.1348, -2280.2791, 0.0000, 1694.5891, -1694.5891, 3443.7949, 0.0002, -0.1155, 0.1155, 0.4764, 0.1835, 121.2918, 121.2526
786003, 1329909240, 2.8559, 2283.1348, -2280.2791, 0.0000, 1694.5872, -1694.5872, 3443.7930, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.5015, 121.1546
786002, 1329909180, 2.8559, 2283.1348, -2280.2791, 0.0000, 1694.5852, -1694.5852, 3443.7910, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.4141, 121.3183
786001, 1329909120, 2.8559, 2283.1348, -2280.2791, 0.0000, 1694.5834, -1694.5834, 3443.7891, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.5175, 121.4314
786000, 1329909060, 2.8559, 2283.1348, -2280.2791, 0.0000, 1694.5814, -1694.5814, 3443.7874, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.6927, 121.4828
785999, 1329909000, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5795, -1694.5795, 3443.7854, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.6357, 121.5007
785998, 1329908940, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5775, -1694.5775, 3443.7834, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.5654, 121.4853
785997, 1329908880, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5756, -1694.5756, 3443.7815, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 120.8418, 120.7854
785996, 1329908820, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5737, -1694.5737, 3443.7795, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 120.9151, 120.7205
785995, 1329908760, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5718, -1694.5718, 3443.7776, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 120.9452, 120.8951
785994, 1329908700, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5698, -1694.5698, 3443.7756, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.1384, 121.0779
785993, 1329908640, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5679, -1694.5679, 3443.7737, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.2673, 121.1448
785992, 1329908580, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5660, -1694.5660, 3443.7717, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.1158, 120.7501
785991, 1329908520, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5641, -1694.5641, 3443.7700, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.2419, 121.0162
785990, 1329908460, 2.8558, 2283.1348, -2280.2791, 0.0000, 1694.5621, -1694.5621, 3443.7681, 0.0002, -0.1153, 0.1153, 0.4759, 0.1415, 121.4649, 121.4177
1