|
You're running .RunWorkAsync() regardless of .IsBusy() on button click.
CancelAsync() implies .... async.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Quote: IDE: Integrated development environment
C#, Visual Studio 2019, Windows10Pro in
2.1
Exception: An object reference does not point to an instance of an object.
Quote: using System;
using System.IO;
using System.Text;
// - using System.Text.StringBuilder;
using System.Globalization;
using System.Windows.Forms;
using System.Threading;
// using Microsoft.CSharp;
namespace WFA_BankPercent
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
//- string m_sWork44 = ""; // C# требует инициализации
private void Form7_Load(object sender, EventArgs e)
{
}
// below on forum www.codeproject.com 29032022
private void button21_Click(object sender, EventArgs e)
{
string line = System.String.Empty;
string s_sourse = System.String.Empty;
bool b_beforeRead = false;
bool b_afterRead = false;
string s_begin = "Begin";
string s_end = "End";
bool b_begin = false;
bool b_end = false;
int i_begin = 0;
int i_end = 0;
try
{
StreamReader sr = new StreamReader(@"D:\Input_my.txt");
StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8);
line = sr.ReadLine();
byte k = 0; // temporary for debug
while (line != null)
{
// k++; // temporary
// //+if (k > 6) // read only the first 6 lines of the input file
////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception
// if (k > 18) // 0 lines were written to the output file, Exception was
// goto Close_my;
if ((line.Trim().Length == 0))
goto label_read;
b_begin = line.Contains(s_begin);
b_end = line.Contains(s_end);
if (b_begin && b_end)
{
i_begin = line.IndexOf(s_begin);
i_end = line.IndexOf(s_end);
s_sourse = line.Substring(0, i_begin);
MessageBox.Show("in line found 'Begin' and 'End'\n" + line);
goto label_wr;
}
else
{
s_sourse = line;
goto label_wr;
}
goto label_read;
label_wr:
sw.WriteLine(s_sourse);
label_read:
b_beforeRead = line.Contains(s_begin);
line = sr.ReadLine();
b_afterRead = line.Contains(s_end);
} // end while (line != null)
Close_my:
line = sr.ReadLine();
MessageBox.Show("Does it get here?" + s_sourse);
sr.Close();
sw.Close();
} // end try
catch (Exception e1)
{
MessageBox.Show("2.1 Exception: " + e1.Message);
}
finally
{
MessageBox.Show("3.Executing finally block.");
}
} // end button21
}
}
|
|
|
|
|
|
 As I said last time you asked this:
Quote: This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.
We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!
Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.
But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
Use the debugger. We can't do that for you!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
label_read:
b_beforeRead = line.Contains(s_begin);
line = sr.ReadLine();
b_afterRead = line.Contains(s_end);
In the above code, if ReadLine hits the end of the input file then line will be null . But this code is so badly constructed it needs a complete rework.
|
|
|
|
|
...which JSOP wrote for him and he won't use it...
I smell a help vampire in the making.
|
|
|
|
|
Dave Kreskowiak wrote: help vampire My tech vocabulary just grew.
Software Zen: delete this;
|
|
|
|
|
We've had a few of them over the years.
|
|
|
|
|
Yes, I tested your code and saw the error. I do not think writing from one text file to another should require the volume of codes I saw, I may be wrong, even when you need to filter some lines.
If I know precisely what you want to do, I can help you, I did something similar before.
You may give detailed explanation of what you want to achieve.
|
|
|
|
|
//Try this procedure and manipulate it
public void RewriteFile(string SourceFileFullName, string TargetFileFullName, string UnWantedFilterString = "")
{
//UnWantedFilterString variable allows a line to be eliminated in the target file if it is contained in the line, this can be fine-tuned depending on your need.
//SourceFileFullName and TargetFileFullName should be full file path and name
if (!File.Exists(SourceFileFullName))
{
MessageBox.Show("Source File Does Not Exist, Confirm on the Disk.");
return;
}
string CurrentLine = "";
string CurrentLineTrimedUcase = "";
string UnWantedFilterUpperTrimed = UnWantedFilterString.Trim().ToUpper(); //This is string of anything you do not want to eliminate a line
try
{
StringBuilder sb2 = new StringBuilder("");
using (StreamReader sr = File.OpenText(SourceFileFullName))
{
while ((CurrentLine = sr.ReadLine()) != null)
{
CurrentLineTrimedUcase = CurrentLine.Trim().ToUpper().Replace("\t", ""); //Avoid tabs, trim and change to upper case
if (UnWantedFilterUpperTrimed == "") //Test for unwated Filter string
{
sb2.AppendLine(CurrentLine); // Append line string to StringBuilder if unwanted filter empty
}
else
{
if (CurrentLineTrimedUcase.IndexOf(UnWantedFilterUpperTrimed) < 0)
{
sb2.AppendLine(CurrentLine); // Only Append line string to StringBuilder if unwanted filter not in string
}
}
}
}
if (sb2.Length == 0)//Empty file will not be written
{
MessageBox.Show("Empty File will not be re-written");
return;
}
//Delete target file if it exists
if (File.Exists(TargetFileFullName))
{
File.Delete(TargetFileFullName);
}
//Write file now
using (StreamWriter sw = File.CreateText(TargetFileFullName))
{
sw.Write(sb2);
}
MessageBox.Show("File Written Successfully");
return;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + " --- File Not Written");
}
}
|
|
|
|
|
I'm trying to write an app that allowed me to copy data from an Android app to a C# app via USB. I will probably write the data to a json file and copy the file to my C# app, either by sending it from Android or copying it from C#.
I found this api but it looks old and I can't find any documentation.
Does anyone know how I can send or copy a file from Android app to a C# app via USB?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 31-Mar-22 1:02am.
|
|
|
|
|
If you connect the Android device to a PC via USB then it should appear as a new disk drive. A quick test with your device should confirm that and you can then see whether you can access the files.
|
|
|
|
|
I thought so too, but it doesn't show up. And I have Developer Mode and USB Debugging turned on
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
That has always worked for me, but see also @OriginalGriff's comments below.
|
|
|
|
|
Richard is right, but ... it may depend on how the Android device is configured
.
There are two ways Android can connect to a PC - MTP for media transfer (pictures and videos), or UMS (for files): Android USB Connections Explained: MTP, PTP, and USB Mass Storage[^]
If the device is selected to "Charge only" or MTP, you won't see the device as a file source.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks. I'll look into it
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I got it now. I went into Settings => USB Preferences and selected "File Transfer". Now I can see the tablet and all the folders under it.
Thanks for pointing me in the right direction.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi all,
I am receiving errors when executing below code.
The code asks me how many items the array has, and then let me fill the array with input.
The problem is whenever I input letters instead of numers I get an exception.
System.FormatException: 'Input string was not in a correct format.
The error happens at the following lines
int size = Convert.ToInt32(Console.ReadLine());
&
array[i] = Convert.ToInt32(Console.ReadLine());
Now I know that I might be using the int.TryParse method, by I do not know how to implement it.
Of perhaps when the input is not valid, give an error like
Console.Write("Wrong input, only numbers");
My code:
Console.Write("How many items in the array ");
int size = Convert.ToInt32(Console.ReadLine());
int[] array = new int[size];
Console.WriteLine("Enter {0} array items", size);
for (int i = 0; i < size; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nThe following items are in the array: ");
for (int i = 0; i < size; i++)
{
Console.Write("{0} ", array[i]);
}
Console.ReadLine();
|
|
|
|
|
Yeah, you cannot convert strings with letters to an integer since there is no logical conversion.
The documentation on Integer.TryParse can be found here[^] complete with examples and you can even run the example code in the browser and play around with it.
|
|
|
|
|
All fine. Thanks.
modified 26-Mar-22 18:18pm.
|
|
|
|
|
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
So how to generate 5 random numbers that fall into the range between 0 and 10000 and assign them to an array? Thanks.
|
|
|
|
|
Think about it: how do you generate one random number? Hint: see the Random class ...
Get that working, and you can then generate as many as you need.
Get that working, and you can store them in an array.
Get that working, and you can start on the rest of the task.
As the link I originally gave you explains: take a "difficult task" and break it into smaller ones. The smaller ones are "doable" on their own, and doing all of those means you do the "impossible" task as well!
Read the article, and see how far you can get.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I made the initial code as below:
using System;
public class Program
{
public static void Main(string[] args)
{
int N = 10;
int[] array = new int[N];
Random rnd = new Random();
for (int i = 0; i < N; i++)
{
array[i] = rnd.Next(10000);
}
for (int j = 0; j < N; j++)
{
Console.Write(array[j])
}
}
}
But it fails to display any code. Can you please help? Thanks.
|
|
|
|