473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Splitting a String Issues

Hello. I am trying to split a string. The string is "lastname, firstname". I need to obviously split the two up. I keep trying to rewrite my code and at this point am not sure of why am I getting the error. The string is coming from the last page of the website. It is telling me in my error "Object reference not set to an instance of an object " refering to line 6.
After looking at many examples I still can't see what the issue is.

Expand|Select|Wrap|Line Numbers
  1.         string firstName;
  2.         string lastName;
  3.  
  4.         string name = Request.QueryString["name"];
  5.  
  6.         string[] nameSplit = name.Split(',');
  7.  
  8.         firstName = nameSplit[0];
  9.         lastName = nameSplit[1];
  10.  
  11.  

I appreciate any feedback. Also if there is an easier way to do this that would be greatly appreciated too. Thanks.
Jun 22 '07 #1
5 915
Lokean
71
Try something along this line


string[] a;
string b = "next, for";
a = b.Split(",");

when you use the split function, the array is called implicitly. If you call it explicitly, you get that error.

i.e. the above code is correct, the code below would generate an error

string[] a;
string b = "next, for";
a[] = b.Split(",");


string firstName;
string lastName;
string nameSplit[];

string name = Request.QueryString["name"];

nameSplit = name.Split(',');

firstName = nameSplit[0];
lastName = nameSplit[1];


in your example, just omit the declaration
Jun 22 '07 #2
Thanks. I tried that and I'm still getting the same error.
Jun 22 '07 #3
Lokean
71
Thanks. I tried that and I'm still getting the same error.
could you post your code from the top? Start with the "using", if you're not referencing the right libraries, that might cause the problem.
Jun 22 '07 #4
Plater
7,872 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1.         string name = Request.QueryString["name"];
  2.  
I would be more suspect of this line returning an empty string.
You should be doing much more error checking/correction there
Jun 22 '07 #5
nateraaaa
663 Expert 512MB
Hello. I am trying to split a string. The string is "lastname, firstname". I need to obviously split the two up. I keep trying to rewrite my code and at this point am not sure of why am I getting the error. The string is coming from the last page of the website. It is telling me in my error "Object reference not set to an instance of an object " refering to line 6.
After looking at many examples I still can't see what the issue is.

Expand|Select|Wrap|Line Numbers
  1.         string firstName;
  2.         string lastName;
  3.  
  4.         string name = Request.QueryString["name"];
  5.  
  6.         string[] nameSplit = name.Split(',');
  7.  
  8.         firstName = nameSplit[0];
  9.         lastName = nameSplit[1];
  10.  
  11.  

I appreciate any feedback. Also if there is an easier way to do this that would be greatly appreciated too. Thanks.
When referencing querystrings in your code you need to always check to see if the querystring value is != null before you try to access the value.

Expand|Select|Wrap|Line Numbers
  1. if(Request.QueryString["name"] != null)
  2. {
  3.         string name = Request.QueryString["name"];
  4.         if(name!=null)
  5.         {
  6.             string[] nameSplit = name.Split(',');
  7.  
  8.             firstName = nameSplit[0];
  9.             lastName = nameSplit[1];
  10.         }
  11.  
  12. }
This should keep you from getting the error that you mentioned.

Nathan
Jun 22 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William Ahern | last post by:
I'm looking for resources on splitting and merging XML trees. Specifically, on methods to pare large XML documents into smaller documents which can be merged later. Off of the top of my head, I...
26
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i don´t have a lot of success. Can anybody help me (and understand my english :-))? I have a...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
13
by: Pedro Pinto | last post by:
Hi there. I'm trying to do the following. I have a string, and i want to separate it into other halves. This is how it should be: char string = "test//test2//test3"; were // is the part...
8
by: ronrsr | last post by:
I have a single long string - I'd like to split it into a list of unique keywords. Sadly, the database wasn't designed to do this, so I must do this in Python - I'm having some trouble using the...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.